Skip to content

Commit aa58e49

Browse files
eltharinfabpot
authored andcommitted
[Routing] Add alias in {foo:bar} syntax in route parameter
1 parent cb1eead commit aa58e49

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)