diff --git a/src/Illuminate/Foundation/helpers.php b/src/Illuminate/Foundation/helpers.php index 9757182626f2..556498168fce 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 = enum_value($name); + } + + $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() ?? $name; + } +} + 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..fe12d3c0e6f0 100755 --- a/src/Illuminate/Routing/Route.php +++ b/src/Illuminate/Routing/Route.php @@ -925,6 +925,43 @@ 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().' + ); + } + + $label = enum_value($label); + + if (! is_string($label)) { + throw new InvalidArgumentException('Enum must be string backed.'); + } + + $this->action['label'] = $label; + + return $this; + } + /** * Set the handler for the route. * 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).''; ?>"; + } +}