milenmk/laravel-route-label

Add label support to Laravel routes with routeLabel() helper and Blade @routeLink directive

1.0.1 2025-09-11 02:32 UTC

This package is auto-updated.

Last update: 2025-09-11 02:32:44 UTC


README

Latest Version on Packagist Total Downloads GitHub User's stars Laravel 10 Support PHP Version Support License Contributions Welcome Sponsor me

A tiny Laravel package that lets you attach human‑friendly labels to your routes and use them in views via a helper or a Blade directive.

  • Route macro: ->label('My Label') (supports string‑backed Enums)
  • Localizing the label You can also pass a translatable string ->label(__('MyLabel')) or key ->label(__('routes.home')) for a label
  • Helper: routeLabel('route.name') → label or route name fallback
  • Blade directive: @routeLink('route.name')<a href="/...">Label</a>
  • Zero config: auto‑discovered service provider, no publishing required

Requirements

  • PHP: 8.2, 8.3, 8.4
  • Laravel: 10.x, 11.x, 12.x (Illuminate Support/View/Routing)

Installation

composer require milenmk/laravel-route-label

No further setup is needed (package discovery enabled).

Quick Start

1) Define routes with labels

use Illuminate\Support\Facades\Route;

Route::get('/users', [UserController::class, 'index'])
    ->name('users.index')
    ->label('Users'); // attach a human-friendly label

You must call ->name() before ->label().

2) Use the label in Blade

{{-- Helper usage --}}
<a href="{{ route('users.index') }}">{{ routeLabel('users.index') }}</a>

{{-- Or use the Blade directive --}}
@routeLink('users.index')

The directive compiles to an anchor tag with the route URL and label.

Enum Support

You can use string‑backed Enums for labels:

enum RouteLabel: string {
    case Users = 'Users';
}

Route::get('/users', [UserController::class, 'index'])
    ->name('users.index')
    ->label(RouteLabel::Users); // Enum is accepted

For route names (Laravel expects a string), pass the enum value:

enum RouteName: string {
    case UsersIndex = 'users.index';
}

Route::get('/users', [UserController::class, 'index'])
    ->name(RouteName::UsersIndex->value)
    ->label('Users');

Note: The @routeLink() directive expects a string route name (not an Enum), because it internally calls route().

Helper Reference

  • routeLabel(string|BackedEnum $name): ?string
    • Returns the route label if set.
    • Returns the route name when no label is set.
    • Returns null if the route does not exist.

Usage examples:

routeLabel('users.index');             // "Users" (if labeled), otherwise "users.index"
routeLabel(RouteName::UsersIndex);     // Works with string-backed Enum

In Blade:

{{ routeLabel('users.index') ?? 'Unknown route' }}

Errors and Edge Cases

  • Calling ->label() before ->name() will throw a LogicException.
  • Passing a non string‑backed Enum to ->label() will throw an InvalidArgumentException.
  • Unknown routes: routeLabel() returns null → use the null coalescing operator to provide a fallback.

Why use route labels?

  • Consistency: keep link texts centralized alongside routes.
  • Localization friendly: prepare for i18n by mapping names to labels.
  • Cleaner views: no more hard‑coded strings scattered in templates.

Changelog

Please see CHANGELOG.md for recent changes.

Contributing

  • Pull requests and issues are welcome at GitHub.
  • Follow PSR‑12 and run linters/tests before submitting.

Run tests locally

vendor/bin/phpunit

Support My Work

If this package saves you time, you can support ongoing development:
👉 Become a Patron

Other Packages

Check out my other Laravel packages:

License

This package is licensed under the MIT License. See the LICENSE file for details.

Disclaimer

This package is provided "as is", without warranty of any kind, express or implied, including but not limited to warranties of merchantability, fitness for a particular purpose, or noninfringement. The author(s) make no guarantees regarding the accuracy, reliability, or completeness of the code, and shall not be held liable for any damages or losses arising from its use. Please ensure you thoroughly test this package in your environment before deploying it to production.