Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a3b8666

Browse files
committed
Documented the inline requirements and default values for routes
1 parent 1e71c35 commit a3b8666

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

routing.rst

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,72 @@ URL Route Parameters
238238
``/blog/yay-routing`` ``blog_show`` ``$slug`` = ``yay-routing``
239239
======================== ============= ===============================
240240

241+
If you prefer, requirements can be inlined in each placeholder using the syntax
242+
``{placeholder_name<requirements>}``. This feature makes configuration more
243+
concise, but it can decrease route readability when requirements are complex:
244+
245+
.. configuration-block::
246+
247+
.. code-block:: php-annotations
248+
249+
// src/Controller/BlogController.php
250+
namespace App\Controller;
251+
252+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
253+
use Symfony\Component\Routing\Annotation\Route;
254+
255+
class BlogController extends Controller
256+
{
257+
/**
258+
* @Route("/blog/{page<\d+>}", name="blog_list")
259+
*/
260+
public function list($page)
261+
{
262+
// ...
263+
}
264+
}
265+
266+
.. code-block:: yaml
267+
268+
# config/routes.yaml
269+
blog_list:
270+
path: /blog/{page<\d+>}
271+
controller: App\Controller\BlogController::list
272+
273+
.. code-block:: xml
274+
275+
<!-- config/routes.xml -->
276+
<?xml version="1.0" encoding="UTF-8" ?>
277+
<routes xmlns="http://symfony.com/schema/routing"
278+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
279+
xsi:schemaLocation="http://symfony.com/schema/routing
280+
http://symfony.com/schema/routing/routing-1.0.xsd">
281+
282+
<route id="blog_list" path="/blog/{page<\d+>}"
283+
controller="App\Controller\BlogController::list" />
284+
285+
<!-- ... -->
286+
</routes>
287+
288+
.. code-block:: php
289+
290+
// config/routes.php
291+
use Symfony\Component\Routing\RouteCollection;
292+
use Symfony\Component\Routing\Route;
293+
use App\Controller\BlogController;
294+
295+
$routes = new RouteCollection();
296+
$routes->add('blog_list', new Route('/blog/{page<\d+>}', array(
297+
'_controller' => [BlogController::class, 'list'],
298+
)));
299+
300+
// ...
301+
302+
return $routes;
303+
304+
.. versionadded:: 4.1
305+
The feature to inline requirements was introduced in Symfony 4.1.
306+
241307
To learn about other route requirements - like HTTP method, hostname and dynamic
242308
expressions - see :doc:`/routing/requirements`.
243309

@@ -331,6 +397,78 @@ So how can you make ``blog_list`` once again match when the user visits
331397
Now, when the user visits ``/blog``, the ``blog_list`` route will match and
332398
``$page`` will default to a value of ``1``.
333399

400+
As it happens with requirements, default values can also be inlined in each
401+
placeholder using the syntax ``{placeholder_name?default_value}``. This feature
402+
is compatible with inlined requirements, so you can inline both in a single
403+
placeholder:
404+
405+
.. configuration-block::
406+
407+
.. code-block:: php-annotations
408+
409+
// src/Controller/BlogController.php
410+
namespace App\Controller;
411+
412+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
413+
use Symfony\Component\Routing\Annotation\Route;
414+
415+
class BlogController extends Controller
416+
{
417+
/**
418+
* @Route("/blog/{page<\d+>?1}", name="blog_list")
419+
*/
420+
public function list($page)
421+
{
422+
// ...
423+
}
424+
}
425+
426+
.. code-block:: yaml
427+
428+
# config/routes.yaml
429+
blog_list:
430+
path: /blog/{page<\d+>?1}
431+
controller: App\Controller\BlogController::list
432+
433+
.. code-block:: xml
434+
435+
<!-- config/routes.xml -->
436+
<?xml version="1.0" encoding="UTF-8" ?>
437+
<routes xmlns="http://symfony.com/schema/routing"
438+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
439+
xsi:schemaLocation="http://symfony.com/schema/routing
440+
http://symfony.com/schema/routing/routing-1.0.xsd">
441+
442+
<route id="blog_list" path="/blog/{page <\d+>?1}"
443+
controller="App\Controller\BlogController::list" />
444+
445+
<!-- ... -->
446+
</routes>
447+
448+
.. code-block:: php
449+
450+
// config/routes.php
451+
use Symfony\Component\Routing\RouteCollection;
452+
use Symfony\Component\Routing\Route;
453+
use App\Controller\BlogController;
454+
455+
$routes = new RouteCollection();
456+
$routes->add('blog_list', new Route('/blog/{page<\d+>?1}', array(
457+
'_controller' => [BlogController::class, 'list'],
458+
)));
459+
460+
// ...
461+
462+
return $routes;
463+
464+
.. tip::
465+
466+
To give a ``null`` default value to any placeholder, add nothing after the
467+
``?`` character (e.g. ``/blog/{page?}``).
468+
469+
.. versionadded:: 4.1
470+
The feature to inline default values was introduced in Symfony 4.1.
471+
334472
Listing all of your Routes
335473
--------------------------
336474

0 commit comments

Comments
 (0)