Skip to content
Closed
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
24 changes: 24 additions & 0 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class BladeCompiler extends Compiler implements CompilerInterface
Concerns\CompilesLayouts,
Concerns\CompilesLoops,
Concerns\CompilesRawPhp,
Concerns\CompilesRoutes,
Concerns\CompilesSessions,
Concerns\CompilesStacks,
Concerns\CompilesStyles,
Expand Down
16 changes: 16 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesRoutes
{
/**
* Compile the routeLink statements into valid PHP.
*
* Usage: @routeLink('home')
*/
protected function compileRouteLink($expression)
{
return "<?php echo '<a href=\"'.route($expression).'\">'.routeLabel($expression).'</a>'; ?>";
}
}