Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -995,6 +995,136 @@ convert them automatically to their scalar values. | ||
} | ||
} | ||
Mapping Parameters | ||
~~~~~~~~~~~~~~~~~~ | ||
By default, the route parameter (``{slug}`` for example) is the name of the argument | ||
injected to the controller method (``$slug``). | ||
Member
| ||
You can change this behavior and define a mapping between route parameter and | ||
an argument name with ``{route_parameter_name:controller_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:articleSlug}', name: 'blog_show')] | ||
public function show(string $articleSlug): Response | ||
{ | ||
// $articleSlug will be equal to the dynamic part of the URL | ||
// e.g. at /blog/yay-routing, then $articleSlug='yay-routing' | ||
// ... | ||
} | ||
} | ||
.. code-block:: yaml | ||
# config/routes.yaml | ||
blog_show: | ||
path: /blog/{slug:articleSlug} | ||
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:articleSlug}" | ||
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:articleSlug}') | ||
->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:articleData}/{slug:articleData}', name: 'blog_show')] | ||
public function show(array $articleData): Response | ||
{ | ||
// $articleData will equal the dynamic part of the URL | ||
// e.g. at /blog/12/yay-routing, then $articleData=['id' => '12', 'slug' => 'yay-routing'] | ||
// ... | ||
} | ||
} | ||
.. code-block:: yaml | ||
# config/routes.yaml | ||
blog_show: | ||
path: /blog/{id:articleData}/{slug:articleData} | ||
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:articleData}/{slug:articleData}" | ||
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:articleData}/{slug:articleData}') | ||
->controller([BlogController::class, 'show']) | ||
; | ||
}; | ||
.. versionadded:: 7.1 | ||
The mapping of route parameters was introduced in Symfony 7.1. | ||
Special Parameters | ||
~~~~~~~~~~~~~~~~~~ | ||
Uh oh!
There was an error while loading.Please reload this page.