From 543f12f27224607e043400e5ae4b76aff064f069 Mon Sep 17 00:00:00 2001 From: Milen Karaganski Date: Wed, 10 Sep 2025 19:36:12 +0300 Subject: [PATCH 1/2] This commits introduces standardized label functionality to Route class and a helper method `routeLabel()` to get the label for a given route. --- src/Illuminate/Foundation/helpers.php | 24 ++++++++++++++++++ src/Illuminate/Routing/Route.php | 35 +++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/Illuminate/Foundation/helpers.php b/src/Illuminate/Foundation/helpers.php index 9757182626f2..65093bf1541e 100644 --- a/src/Illuminate/Foundation/helpers.php +++ b/src/Illuminate/Foundation/helpers.php @@ -872,6 +872,30 @@ function route($name, $parameters = [], $absolute = true): string } } +if (! function_exists('routeLabel')) { + /** + * Get the label of a named route. + * + * @param \BackedEnum|string $name + * @return string|null + */ + function routeLabel($name): ?string + { + if ($name instanceof BackedEnum) { + $name = $name->value; + } + + $route = app('router')->getRoutes()->getByName($name); + + if (! $route) { + return null; // route not found at all + } + + // Prefer label, but fall back to route name + return $route->getLabel() ?? $route->getName(); + } +} + if (! function_exists('secure_asset')) { /** * Generate an asset path for the application. diff --git a/src/Illuminate/Routing/Route.php b/src/Illuminate/Routing/Route.php index 355d18b0e058..2d3f7cf459cc 100755 --- a/src/Illuminate/Routing/Route.php +++ b/src/Illuminate/Routing/Route.php @@ -925,6 +925,41 @@ public function named(...$patterns) return false; } + /** + * Get the label of the route instance. + * + * @return string|null + */ + public function getLabel() + { + return $this->action['label'] ?? null; + } + + /** + * Add or change the route label. + * + * @param \BackedEnum|string $name + * @return $this + * + * @throws \InvalidArgumentException + */ + public function label($label) + { + if (! isset($this->action['as'])) { + throw new \LogicException( + 'Cannot set a route label without a route name. Use ->name() before ->label().' + ); + } + + if ($label instanceof BackedEnum && ! is_string($label = $label->value)) { + throw new InvalidArgumentException('Enum must be string backed.'); + } + + $this->action['label'] = $label; + + return $this; + } + /** * Set the handler for the route. * From d918127fe58da6275f33647a56be17f8396f88ad Mon Sep 17 00:00:00 2001 From: Milen Karaganski Date: Thu, 11 Sep 2025 04:39:34 +0300 Subject: [PATCH 2/2] - Converts route parameter label retrieval to use enum_value helper function. - Use enum_value() for enum handling internally - Introduce CompilesRoutes concern with @routeLink Blade directive --- src/Illuminate/Foundation/helpers.php | 4 ++-- src/Illuminate/Routing/Route.php | 4 +++- src/Illuminate/View/Compilers/BladeCompiler.php | 1 + .../View/Compilers/Concerns/CompilesRoutes.php | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 src/Illuminate/View/Compilers/Concerns/CompilesRoutes.php diff --git a/src/Illuminate/Foundation/helpers.php b/src/Illuminate/Foundation/helpers.php index 65093bf1541e..556498168fce 100644 --- a/src/Illuminate/Foundation/helpers.php +++ b/src/Illuminate/Foundation/helpers.php @@ -882,7 +882,7 @@ function route($name, $parameters = [], $absolute = true): string function routeLabel($name): ?string { if ($name instanceof BackedEnum) { - $name = $name->value; + $name = enum_value($name); } $route = app('router')->getRoutes()->getByName($name); @@ -892,7 +892,7 @@ function routeLabel($name): ?string } // Prefer label, but fall back to route name - return $route->getLabel() ?? $route->getName(); + return $route->getLabel() ?? $name; } } diff --git a/src/Illuminate/Routing/Route.php b/src/Illuminate/Routing/Route.php index 2d3f7cf459cc..fe12d3c0e6f0 100755 --- a/src/Illuminate/Routing/Route.php +++ b/src/Illuminate/Routing/Route.php @@ -951,7 +951,9 @@ public function label($label) ); } - if ($label instanceof BackedEnum && ! is_string($label = $label->value)) { + $label = enum_value($label); + + if (! is_string($label)) { throw new InvalidArgumentException('Enum must be string backed.'); } diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index d63cc7bce0d5..6fd7febc1eea 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -33,6 +33,7 @@ class BladeCompiler extends Compiler implements CompilerInterface Concerns\CompilesLayouts, Concerns\CompilesLoops, Concerns\CompilesRawPhp, + Concerns\CompilesRoutes, Concerns\CompilesSessions, Concerns\CompilesStacks, Concerns\CompilesStyles, diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesRoutes.php b/src/Illuminate/View/Compilers/Concerns/CompilesRoutes.php new file mode 100644 index 000000000000..2db15158cb1b --- /dev/null +++ b/src/Illuminate/View/Compilers/Concerns/CompilesRoutes.php @@ -0,0 +1,16 @@ +'.routeLabel($expression).''; ?>"; + } +}