Skip to content

[Routing] Add {foo:bar} syntax to define a mapping between a route parameter and its corresponding request attribute #19869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,136 @@ convert them automatically to their scalar values.
}
}

Mapping Parameters
~~~~~~~~~~~~~~~~~~

By default, the name of the variable part (``{slug}`` for example) is the
argument injected name to the method (``$slug``).

You can change this behavior and define mapping between variable part and
argument name with ``{variable_part_name:argument_name}``:

.. configuration-block::

.. code-block:: php-attributes

// src/Controller/BlogController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class BlogController extends AbstractController
{
// ...

#[Route('/blog/{slug:article}', name: 'blog_show')]
public function show(string $article): Response
{
// $article will equal the dynamic part of the URL
// e.g. at /blog/yay-routing, then $article='yay-routing'

// ...
}
}

.. code-block:: yaml

# config/routes.yaml
blog_show:
path: /blog/{slug:article}
controller: App\Controller\BlogController::show

.. code-block:: xml

<!-- config/routes.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
https://symfony.com/schema/routing/routing-1.0.xsd">

<route id="blog_show" path="/blog/{slug:article}"
controller="App\Controller\BlogController::show"/>
</routes>

.. code-block:: php

// config/routes.php
use App\Controller\BlogController;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

return function (RoutingConfigurator $routes): void {
$routes->add('blog_show', '/blog/{slug:article}')
->controller([BlogController::class, 'show'])
;
};

When two or more variable parts target the same argument name, argument will be
an array:

.. configuration-block::

.. code-block:: php-attributes

// src/Controller/BlogController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class BlogController extends AbstractController
{
// ...

#[Route('/blog/{id:article}/{slug:article}', name: 'blog_show')]
public function show(array $article): Response
{
// $article will equal the dynamic part of the URL
// e.g. at /blog/12/yay-routing, then $article=['id' => '12', 'slug' => 'yay-routing']

// ...
}
}

.. code-block:: yaml

# config/routes.yaml
blog_show:
path: /blog/{id:article}/{slug:article}
controller: App\Controller\BlogController::show

.. code-block:: xml

<!-- config/routes.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
https://symfony.com/schema/routing/routing-1.0.xsd">

<route id="blog_show" path="/blog/{id:article}/{slug:article}"
controller="App\Controller\BlogController::show"/>
</routes>

.. code-block:: php

// config/routes.php
use App\Controller\BlogController;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

return function (RoutingConfigurator $routes): void {
$routes->add('blog_show', '/blog/{id:article}/{slug:article}')
->controller([BlogController::class, 'show'])
;
};

.. versionadded:: 7.1

The mapping of route parameters was introduced in Symfony 7.1.

Special Parameters
~~~~~~~~~~~~~~~~~~

Expand Down
Loading