Download the PHP package dannypas00/laravel-dynamic-routes without Composer
On this page you can find all versions of the php package dannypas00/laravel-dynamic-routes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dannypas00/laravel-dynamic-routes
More information about dannypas00/laravel-dynamic-routes
Files in dannypas00/laravel-dynamic-routes
Package laravel-dynamic-routes
Short Description A dynamic route service provider that maps route files based on their path
License MIT
Informations about the package laravel-dynamic-routes
Laravel dynamic routes
Laravel dynamic routes is a simple RouteServiceProvider that dynamically resolves routes from route files based on their placement within the filesystem. Route names and prefixes are dynamically allocated to the routes resolved by this provider, and middleware (groups) can be assigned on a directory level.
Installation
Implementation
Implementation is very simple; just change the default RouteServiceProvider import in your
project's App\Providers\RouteServiceProvider
from Illuminate\Foundation\Support\Providers\RouteServiceProvider
to DannyPas00\LaravelDynamicRoutes\RouteServiceProvider
:
Example
Say we have a directory structure like so:
In vanilla laravel, you would need to add code to the RouteServiceProvider to register each of these files individually.
This made managing large projects cumbersome because you would either end up with a couple very big route files (such as
web.php or api.php), or many small ones that all require a separate piece of code in the RouteServiceProvider,
cluttering the provider instead.
With dynamic routing however, all of these route files will be registered without any additional configuration needed.
When this is done, the routes in the route file take on a path associated with the file location.
E.g. the routes registered in the routes/api/rest/users.php
file will all be registered as api/rest/users/{route}
.
The full output of php artisan route:list
would look something like this:
Since the dynamic route provider extends the laravel provider, all methods can still be overwritten, so long as the
dynamic provider's boot()
or routeRegistration()
method is called.
Customizing
The following customization options are available within the route service provider:
Route directory
Setting the ROUTE_DIRECTORY
const in the implementing service provider will change the default routes/
directory.
Root file
Setting the ROOT_FILE
const in the implementing service provider will change which route files will be treated as "
root" files.
Any root file in any directory will not reflect its name into its prefix or path (see root.php in example).
Flatten directories
Any directory added to the FLATTEN_DIRECTORIES
array will be ignored when building paths and prefixes.
Set to just 'web'
by default, any file in the routes/web
directory will not have "web" in its route or prefix.
Middleware matching
For any directory-wide middleware matching (web and api split for example), the matchMiddleware
function can be
overwritten.
It accepts the route's directory as string, and expects a middleware string (or null when not middleware needs to be
set) as a response.