Skip to content

Commit cc168c0

Browse files
committed
feature #59904 [Routing] Add alias in {foo:bar} syntax in route parameter (eltharin)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Routing] Add alias in `{foo:bar}` syntax in route parameter | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT Since symfony/symfony#54720 we can/have to write route parameters with "destination" as slug:bar, but if we have two properties with same name example : `/search-book/{name:author}/{name:category}` we get the error message : Route pattern "/search-book/{name}/{name}" cannot reference variable name "name" more than once. Actually to prevent this error we have to use MapEntity as : ```php public function bookSearch( #[MapEntity(mapping: ['authorName' => 'name'])] Author $author, #[MapEntity(mapping: ['categoryName' => 'name'])] Category $category): Response { ``` and we have to remove Mapped Route Parameters : `#[Route('/search-book/{authorName}/{categoryName}')` This PR proposal is to remove MapEntity attributes and keep Mapped Route Parameters by adding an alias on it : `/search-book/{authorName:author.name}/{categoryName:category.name}` With that, EntityValueResolver will search name in author Entity and name in Category Entity. We can have url with : `{{ path('bookSearch', {authorName: 'KING', categoryName: 'Horror'}) }}` Commits ------- 4e2c6386912 [Routing] Add alias in `{foo:bar}` syntax in route parameter
2 parents fb1bc05 + aa58e49 commit cc168c0

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

EventListener/RouterListener.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,14 @@ public function onKernelRequest(RequestEvent $event): void
117117
$attributes = [];
118118

119119
foreach ($parameters as $parameter => $value) {
120-
$attribute = $mapping[$parameter] ?? $parameter;
120+
if (!isset($mapping[$parameter])) {
121+
$attribute = $parameter;
122+
} elseif (\is_array($mapping[$parameter])) {
123+
[$attribute, $parameter] = $mapping[$parameter];
124+
$mappedAttributes[$attribute] = '';
125+
} else {
126+
$attribute = $mapping[$parameter];
127+
}
121128

122129
if (!isset($mappedAttributes[$attribute])) {
123130
$attributes[$attribute] = $value;

Tests/EventListener/RouterListenerTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,5 +323,49 @@ public static function provideRouteMapping(): iterable
323323
],
324324
],
325325
];
326+
327+
yield [
328+
[
329+
'conference' => ['slug' => 'vienna-2024'],
330+
],
331+
[
332+
'slug' => 'vienna-2024',
333+
'_route_mapping' => [
334+
'slug' => [
335+
'conference',
336+
'slug',
337+
],
338+
],
339+
],
340+
];
341+
342+
yield [
343+
[
344+
'article' => [
345+
'id' => 'abc123',
346+
'date' => '2024-04-24',
347+
'slug' => 'symfony-rocks',
348+
],
349+
],
350+
[
351+
'id' => 'abc123',
352+
'date' => '2024-04-24',
353+
'slug' => 'symfony-rocks',
354+
'_route_mapping' => [
355+
'id' => [
356+
'article',
357+
'id'
358+
],
359+
'date' => [
360+
'article',
361+
'date',
362+
],
363+
'slug' => [
364+
'article',
365+
'slug',
366+
],
367+
],
368+
],
369+
];
326370
}
327371
}

0 commit comments

Comments
 (0)