Adds route registrars as an alternative object based approach to registering routes.
olliecodes/laravel-route-registrars is a Laravel package for adds route registrars as an alternative object based approach to registering routes..
It currently has 23 GitHub stars and 1.084 downloads on Packagist (latest version 1.0.0).
Install it with composer require olliecodes/laravel-route-registrars.
Discover more Laravel packages by olliecodes
or browse all Laravel packages to compare alternatives.
Last updated
This package introduces a clean object based way to define your routes in a Laravel application. A tutorial on the basic premise exists on Laravel news, written by @juststeveking.
Install via composer.
$ composer require olliecodes/laravel-route-registrars
If you're installing this package on a fresh laravel installation, you'll want to run the following command before you do anything with your routes.
php artisan init:routing
This will overwrite the app/Providers/RouteServiceProvider.php file with one compatible with the route registrars, and create default route registrars to replace both routes/web.php and routes/api.php.
If you have an existing application, it is recommended that you read through the Laravel News article that covers this approach, as you're going to have to manually refactor your route service provider and route files.
This package requires the following;
laravel/framework >= 9.0To register new routes, you can either add their definitions to the map method of a RouteRegistrar, or create a new one.
The following command will create a new registrar inside app/Http/Routes.
php artisan make:registrar {name}
The {name} can be any valid class name, or sub-namespace. For example, using Auth\\GuestRoutes will create app/Http/Routes/Auth/GuestRoutes.php.
There are several options available when creating a registrar.
--C|hasChildrenCreate a route registrar that has the MapRouteRegistrars trait so that it can map child registrars.
--W|webCreate the route registrar in app/Http/Routes/Web, an option left in for those of you that are splitting the Web and API routes.
--A|APIExactly the same as the above option, except it creates it in app/Http/Routes/Api,
Any class that wants to register registrars can use the following trait.
OllieCodes\Registrars\Concerns\MapsRouteRegistrars
Once doing so, there are three ways to register. All of them require an instance of the following class.
Illuminate\Contracts\Routing\Registrar
This interface is implemented by the Laravel Router class, so you can use that.
Call the following method with an instance of the laravel router, and an array of fully qualified class names for the registrars.
mapRouteRegistrars(Registrar $router, array $registrars)
Call the following method with an instance of the laravel router, and the fully qualified class name of the registrars.
mapRouteRegistrar(Registrar $router, string $registrar)
Call the following method with an instance of the laravel router, and an array of paths where the registrars are held.
This method will use the order that the files are returned, so please keep route order precedence in mind.
loadRouteRegistrars(Registrar $router, array $paths)