diff --git a/src/Illuminate/Routing/ImplicitRouteBinding.php b/src/Illuminate/Routing/ImplicitRouteBinding.php index 288c7a7b6564..7d92ec9757f9 100644 --- a/src/Illuminate/Routing/ImplicitRouteBinding.php +++ b/src/Illuminate/Routing/ImplicitRouteBinding.php @@ -90,7 +90,9 @@ protected static function resolveBackedEnumsForRoute($route, $parameters) $backedEnumClass = $parameter->getType()?->getName(); - $backedEnum = $backedEnumClass::tryFrom((string) $parameterValue); + $backedEnum = $parameterValue instanceof $backedEnumClass + ? $parameterValue + : $backedEnumClass::tryFrom((string) $parameterValue); if (is_null($backedEnum)) { throw new BackedEnumCaseNotFoundException($backedEnumClass, $parameterValue); diff --git a/tests/Integration/Routing/CategoryBackedEnum.php b/tests/Integration/Routing/CategoryBackedEnum.php new file mode 100644 index 000000000000..2ff3cf6e7f80 --- /dev/null +++ b/tests/Integration/Routing/CategoryBackedEnum.php @@ -0,0 +1,18 @@ + self::People, + 'c02' => self::Fruits, + default => null, + }; + } +} diff --git a/tests/Integration/Routing/Enums.php b/tests/Integration/Routing/Enums.php deleted file mode 100644 index 5a2ba0f5a8b1..000000000000 --- a/tests/Integration/Routing/Enums.php +++ /dev/null @@ -1,9 +0,0 @@ -value; })->middleware('web'); + Route::bind('categoryCode', fn (string $categoryCode) => CategoryBackedEnum::fromCode($categoryCode) ?? abort(404)); + + Route::post('/categories-code/{categoryCode}', function (CategoryBackedEnum $categoryCode) { + return $categoryCode->value; + })->middleware(['web']); + $response = $this->post('/categories/fruits'); $response->assertSee('fruits'); @@ -68,7 +72,7 @@ public function testWithoutRouteCachingEnabled() $response->assertSee('people'); $response = $this->post('/categories/cars'); - $response->assertNotFound(404); + $response->assertNotFound(); $response = $this->post('/categories-default/'); $response->assertSee('fruits'); @@ -78,5 +82,14 @@ public function testWithoutRouteCachingEnabled() $response = $this->post('/categories-default/fruits'); $response->assertSee('fruits'); + + $response = $this->post('/categories-code/c01'); + $response->assertSee('people'); + + $response = $this->post('/categories-code/c02'); + $response->assertSee('fruits'); + + $response = $this->post('/categories-code/00'); + $response->assertNotFound(); } }