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 mapped route parameters and aliases#20812
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
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
e37a9dc
add mapped route parameters and aliases
eltharin36cdca5
corrections
eltharina60587c
Update doctrine.rst
eltharinab4ac88
remove traillingslash and backticks
eltharinc0ead98
MrYamous changes
eltharin23a1f0d
was-were
eltharin286ba1b
update from comments
eltharin63db3a4
remove the
eltharinFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletionsdoctrine.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -861,6 +861,84 @@ 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 | ||
eltharin marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
Mapped route parameters were introduced in Symfony 7.1. | ||
But when two properties have the same name, you will catach an error if you try :: | ||
#[Route('/document/{slug:category}/{id:document}-{slug:document}')] | ||
public function showDocument(Document $document, Category $category): Response | ||
{ | ||
// category entity and document entity have the same property ``slug`` but in the route_parameters we can't have two ``slug`` arguments. | ||
} | ||
In this case we have to return to MapEntiy:: | ||
#[Route('/document/{slugCategory}/{id}-{slugDocument}')] | ||
public function showDocument( | ||
#[MapEntity(mapping: ['slugCategory' => 'slug'])] | ||
Category $category, | ||
#[MapEntity(mapping: ['id' => 'id', 'slugDocument' => 'slug'])] | ||
Document $document, | ||
): Response | ||
{ | ||
// this would result in the following database queries: | ||
// $document = $documentRepository->findOneBy(['id' => 'the id', 'slug' => 'the slug document']); | ||
// $category = $categoryRepository->findOneBy(['slug' => 'the slug category']); | ||
} | ||
As an alternative, you can use Aliased Mapped Route Parameters. | ||
When adding route parameters, you can now define the mapping between the route parameter and the controller argument with an alias:: | ||
#[Route('/document/{slugCategory:category.slug}/{id:document}-{slugDocument:document.slug}')] | ||
public function showDocument(Document $document, Category $category): Response | ||
{ | ||
// this would result in the following database queries: | ||
// $document = $documentRepository->findOneBy(['id' => 'the id', 'slug' => 'the slug document']); | ||
// $category = $categoryRepository->findOneBy(['slug' => 'the slug category']); | ||
} | ||
In this case, _route_mapping keys will be slugCategory and slugDocument, and used by path twig option. | ||
.. code-block:: twig | ||
{{ path('showDocument', {slugCategory: 'invoices', id: 25, slugDocument: 'invoice_CFD025125'}) }} | ||
.. versionadded:: 7.3 | ||
Aliased mapped route parameters were introduced in Symfony 7.3. | ||
Updating an Object | ||
------------------ | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.