Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Closed
Changes fromall commits
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 deletionsrouting.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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``).
Copy link
Member

@nicolas-grekasnicolas-grekasMay 10, 2024
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think we need to be more precise. There are two steps between parameters and controllers:
The routing side does only one thing: populate a parameter named _route_mapping.
Then a listener will use that to map parameters to request attributes.
And then argument resolvers will map to controllers.
Explaining the steps can empower readers.
Not sure what exactly how we should say all this :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks for the info. I just wanted to help with the code which works with this new feature.. :)


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
~~~~~~~~~~~~~~~~~~

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp