Skip to content

[Routing] Add alias in {foo:bar} syntax in route parameter #59904

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

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,14 @@ public function onKernelRequest(RequestEvent $event): void
$attributes = [];

foreach ($parameters as $parameter => $value) {
$attribute = $mapping[$parameter] ?? $parameter;
if (!isset($mapping[$parameter])) {
$attribute = $parameter;
} elseif (\is_array($mapping[$parameter])) {
[$attribute, $parameter] = $mapping[$parameter];
$mappedAttributes[$attribute] = '';
} else {
$attribute = $mapping[$parameter];
}

if (!isset($mappedAttributes[$attribute])) {
$attributes[$attribute] = $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,5 +323,49 @@ public static function provideRouteMapping(): iterable
],
],
];

yield [
[
'conference' => ['slug' => 'vienna-2024'],
],
[
'slug' => 'vienna-2024',
'_route_mapping' => [
'slug' => [
'conference',
'slug',
],
],
],
];

yield [
[
'article' => [
'id' => 'abc123',
'date' => '2024-04-24',
'slug' => 'symfony-rocks',
],
],
[
'id' => 'abc123',
'date' => '2024-04-24',
'slug' => 'symfony-rocks',
'_route_mapping' => [
'id' => [
'article',
'id'
],
'date' => [
'article',
'date',
],
'slug' => [
'article',
'slug',
],
],
],
];
}
}
14 changes: 7 additions & 7 deletions src/Symfony/Component/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,15 +418,15 @@ private function extractInlineDefaultsAndRequirements(string $pattern): string

$mapping = $this->getDefault('_route_mapping') ?? [];

$pattern = preg_replace_callback('#\{(!?)([\w\x80-\xFF]++)(:[\w\x80-\xFF]++)?(<.*?>)?(\?[^\}]*+)?\}#', function ($m) use (&$mapping) {
if (isset($m[5][0])) {
$this->setDefault($m[2], '?' !== $m[5] ? substr($m[5], 1) : null);
$pattern = preg_replace_callback('#\{(!?)([\w\x80-\xFF]++)(:([\w\x80-\xFF]++)(\.[\w\x80-\xFF]++)?)?(<.*?>)?(\?[^\}]*+)?\}#', function ($m) use (&$mapping) {
if (isset($m[7][0])) {
$this->setDefault($m[2], '?' !== $m[6] ? substr($m[7], 1) : null);
}
if (isset($m[4][0])) {
$this->setRequirement($m[2], substr($m[4], 1, -1));
if (isset($m[6][0])) {
$this->setRequirement($m[2], substr($m[6], 1, -1));
}
if (isset($m[3][0])) {
$mapping[$m[2]] = substr($m[3], 1);
if (isset($m[4][0])) {
$mapping[$m[2]] = isset($m[5][0]) ? [$m[4], substr($m[5], 1)] : $mapping[$m[2]] = [$m[4], $m[2]];
}

return '{'.$m[1].$m[2].'}';
Expand Down
25 changes: 24 additions & 1 deletion src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,30 @@ public function testMapping()
'_route' => 'a',
'slug' => 'vienna-2024',
'_route_mapping' => [
'slug' => 'conference',
'slug' => [
'conference',
'slug',
],
],
];
$this->assertEquals($expected, $matcher->match('/conference/vienna-2024'));
}

public function testMappingwithAlias()
{
$collection = new RouteCollection();
$collection->add('a', new Route('/conference/{conferenceSlug:conference.slug}'));

$matcher = $this->getUrlMatcher($collection);

$expected = [
'_route' => 'a',
'conferenceSlug' => 'vienna-2024',
'_route_mapping' => [
'conferenceSlug' => [
'conference',
'slug',
],
],
];
$this->assertEquals($expected, $matcher->match('/conference/vienna-2024'));
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy