Skip to content

Commit cb567d3

Browse files
[11.x] Replace all backed enums with values when building URLs (#51524)
* Replace all enums with values when generating URLs * Update UrlGenerator.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 41409f4 commit cb567d3

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/Illuminate/Routing/UrlGenerator.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,17 @@ public function route($name, $parameters = [], $absolute = true)
525525
public function toRoute($route, $parameters, $absolute)
526526
{
527527
$parameters = collect(Arr::wrap($parameters))->map(function ($value, $key) use ($route) {
528-
$value = $value instanceof UrlRoutable && $route->bindingFieldFor($key)
528+
return $value instanceof UrlRoutable && $route->bindingFieldFor($key)
529529
? $value->{$route->bindingFieldFor($key)}
530530
: $value;
531-
532-
return $value instanceof BackedEnum ? $value->value : $value;
533531
})->all();
534532

533+
array_walk_recursive($parameters, function (&$item) {
534+
if ($item instanceof BackedEnum) {
535+
$item = $item->value;
536+
}
537+
});
538+
535539
return $this->routeUrl()->to(
536540
$route, $this->formatParameters($parameters), $absolute
537541
);

tests/Routing/RoutingUrlGeneratorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,22 @@ public function testRouteGenerationWithBackedEnums()
887887
$this->assertSame('http://www.foo.com/foo/fruits', $url->route('foo.bar', CategoryBackedEnum::Fruits));
888888
}
889889

890+
public function testRouteGenerationWithNestedBackedEnums()
891+
{
892+
$url = new UrlGenerator(
893+
$routes = new RouteCollection,
894+
Request::create('http://www.foo.com/')
895+
);
896+
897+
$namedRoute = new Route(['GET'], '/foo', ['as' => 'foo']);
898+
$routes->add($namedRoute);
899+
900+
$this->assertSame(
901+
'http://www.foo.com/foo?filter%5B0%5D=people&filter%5B1%5D=fruits',
902+
$url->route('foo', ['filter' => [CategoryBackedEnum::People, CategoryBackedEnum::Fruits]]),
903+
);
904+
}
905+
890906
public function testSignedUrlWithKeyResolver()
891907
{
892908
$url = new UrlGenerator(

0 commit comments

Comments
 (0)