annotation/laroute

Auto register routes using PHP attributes for Laravel.

dev-main / 1.x-dev 2025-02-13 07:42 UTC

This package is auto-updated.

Last update: 2025-05-28 14:36:39 UTC


README

Using PHP 8 attributes to automatically generate routes in order to streamline Laravel routing, enhancing developer productivity while maintaining full compatibility with Laravel's routing functionality.

GitHub Tag Total Downloads Packagist Version Packagist PHP Version Support Packagist License

Installation

You can install the package via Composer:

composer require annotation/laroute

Usage

Directories Configurations

'directories' => [
    app_path('Http/Controllers/Backend'),
],

Only specify the scan path, no extended configuration information

Basic Configuration

'directories' => [
     app_path('Http/Controllers/Backend')    => [
         'domain'     => env('LAROUTE_DOMAINS_BACKEND', 'backend.lvh.me'),
         'prefix'     => 'backend',
         'as'         => 'backend.',
         'middleware' => 'web',
         // Only routes matching the pattern in the file are registered
         'only'       => ['*Controller.php'],
         // Except routes from registration pattern matching file
         'except'     => [],
     ],
],

More configurations of domain, prefix, as, and middleware can be specified

Multiple Configurations

'directories' => [
     app_path('Http/Controllers/Enterprise') => [
         [
             'domain'     => env('LAROUTE_DOMAINS_ENTERPRISE', 'enterprise.lvh.me'),
             'prefix'     => 'enterprise',
             'as'         => 'enterprise.',
             'middleware' => [
                 'web',
                 'auth',
             ],
             // Only routes matching the pattern in the file are registered
             'only'       => ['*Controller.php'],
             // Except routes from registration pattern matching file
             'except'     => ['AccountController.php'],
         ], [
             'domain'     => env('LAROUTE_DOMAINS_ENTERPRISE', 'enterprise.lvh.me'),
             'prefix'     => 'enterprise',
             'as'         => 'enterprise.',
             'middleware' => [
                 'web',
             ],
             // Only routes matching the pattern in the file are registered
             'only'       => ['AccountController.php'],
             // Except routes from registration pattern matching file
             'except'     => [],
         ],
     ],
],

The same path can specify multiple groups of configurations, suitable for scenarios such as Auth and Guest routing

Route Discover

Basic usage

use Annotation\Routing\Facades\Route;

Route::discover();

Route discover monitoring

use Annotation\Routing\Facades\Route;

Route::discover(function (array $data, \Closure $next) {
    return $next($data);
});

Gateway Routing

use Annotation\Routing\Facades\Route;

Route::gateWay('gateway.do', function (Request $request) {
    return $request->get('action');
});

Specify the route to get the named source

use Annotation\Routing\Facades\Route;

Route::gateWay('gateway.do', function (Request $request) {
    return $request->get('action');
}, function (Request $request) {
    return $request->get('version');
});

Specify the route to obtain the version source

Command Support

php artisan route.scan:cache

Create a scanned route cache file for faster route registration

php artisan route.scan:clear

Remove the scanned route cache file

License

Nacosvel Contracts is made available under the MIT License (MIT). Please see License File for more information.