Auto register routes using PHP attributes
spatie/laravel-route-discovery is a Laravel package for auto register routes using php attributes.
It currently has 240 GitHub stars and 51.468 downloads on Packagist (latest version 1.6.0).
Install it with composer require spatie/laravel-route-discovery.
Discover more Laravel packages by spatie
or browse all Laravel packages to compare alternatives.
Last updated
// typically in a routes file
Discover::controllers()->in($whateverDirectoryYouPrefer);
Discover::views()->in($whateverDirectoryYouPrefer);
// other routes
Using PHP attributes you can manipulate discovered routes: you can set a route name, add some middleware, or ...
Here's how you would add middleware to a controller whose's route will be auto discovered.
namespace App\Http\Controllers;
use Illuminate\Routing\Middleware\ValidateSignature;
use Spatie\RouteDiscovery\Attributes\Route;
class MyController
{
#[Route(middleware: ValidateSignature::class)]
public function myMethod() { /* ... */ }
}
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You'll find full documentation at the Spatie website.
Discovering routes during each application request may have a small impact on performance. For increased performance, we highly recommend caching your routes as part of your deployment process.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.