Skip to content
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

[Routing] document mapped route parameters #20820

Open
wants to merge 4 commits into
base: 7.2
Choose a base branch
from
Open
Changes from all 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
33 changes: 33 additions & 0 deletions doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,39 @@ control behavior:

The ``message`` option was introduced in Symfony 7.1.

Mapped Route Parameters
~~~~~~~~~~~~~~~~~~~~~~~

When many route parameters are used to find more than one entity,
it is mandatory to use ``#[MapEntity]`` attributes and this can become cumbersome::

#[Route('/document/{slug}/{id}-{name}')]
public function showDocument(
#[MapEntity(mapping: ['slug' => 'slug'])]
Category $category,
#[MapEntity(mapping: ['id' => 'id', 'name' => 'name'])]
Document $document,
): Response
{
// this would result in the following database queries:
// $document = $documentRepository->findOneBy(['id' => 'the id', 'name' => 'the name']);
// $category = $categoryRepository->findOneBy(['slug' => 'the slug']);
}

By using mapped route parameters, you can define the mapping between the route parameter and the controller argument::

#[Route('/document/{slug:category}/{id:document}-{name:document}')]
public function showDocument(Document $document, Category $category): Response
{
// this would result in the following database queries:
// $document = $documentRepository->findOneBy(['id' => 'the id', 'name' => 'the name']);
// $category = $categoryRepository->findOneBy(['slug' => 'the slug']);
}

.. versionadded:: 7.1

Mapped route parameters were introduced in Symfony 7.1.

Updating an Object
------------------

Expand Down