spatie/laravel-route-discovery
Automatically discover Laravel routes by scanning controllers and views instead of manually defining them. Configure discovery in your routes files and use PHP attributes to customize names, middleware, and more for each discovered route.
Under the hood, the package will build up a collection of Spatie\RouteDiscovery\PendingRoutes\PendingRoute instances by looking at the controllers and views in your project. This collection of PendingRoutes can be modified by a PendingRouteTransformer. After that, each action inside a PendingRoute will be registered as a regular Laravel route.
In the route-discovery config file you'll see the registered route transformers.
// in config/route-discovery.php
/*
* After having discovered all controllers, these classes will manipulate the routes
* before registering them to Laravel.
*
* In most cases, you shouldn't change these.
*/
'pending_route_transformers' => [
...Spatie\RouteDiscovery\Config::defaultRouteTransformers(),
//
],
This is the returned value of Spatie\RouteDiscovery\Config::defaultRouteTransformers():
[
Spatie\RouteDiscovery\PendingRouteTransformers\HandleDoNotDiscoverAttribute::class,
Spatie\RouteDiscovery\PendingRouteTransformers\AddControllerUriToActions::class,
Spatie\RouteDiscovery\PendingRouteTransformers\HandleUrisOfNestedControllers::class,
Spatie\RouteDiscovery\PendingRouteTransformers\HandleRouteNameAttribute::class,
Spatie\RouteDiscovery\PendingRouteTransformers\HandleMiddlewareAttribute::class,
Spatie\RouteDiscovery\PendingRouteTransformers\HandleHttpMethodsAttribute::class,
Spatie\RouteDiscovery\PendingRouteTransformers\HandleUriAttribute::class,
Spatie\RouteDiscovery\PendingRouteTransformers\HandleFullUriAttribute::class,
Spatie\RouteDiscovery\PendingRouteTransformers\HandleWheresAttribute::class,
Spatie\RouteDiscovery\PendingRouteTransformers\AddDefaultRouteName::class,
Spatie\RouteDiscovery\PendingRouteTransformers\HandleDomainAttribute::class,
Spatie\RouteDiscovery\PendingRouteTransformers\MoveRoutesStartingWithParametersLast::class,
];
These transformers will handle specific Route attributes, make sure the routes are registered in the correct orders, ...
You can create your own route transformer by letting a class implement the Spatie\RouteDiscovery\PendingRouteTransformers\PendingRouteTransformer interface. Here's how that interface looks like:
use Illuminate\Support\Collection;
use Spatie\RouteDiscovery\PendingRoutes\PendingRoute;
interface PendingRouteTransformer
{
/**
* [@param](https://github.com/param) Collection<PendingRoute> $pendingRoutes
*
* [@return](https://github.com/return) Collection<PendingRoute>
*/
public function transform(Collection $pendingRoutes): Collection;
}
After you've created your transformer, register it in the pending_route_transformers key of the route-discovery config file.
Take a look at one of the default route transformers for an example implementation.
How can I help you explore Laravel packages today?