How to create multiple files for your routes in Laravel
This post is for How to create multiple files for your routes in Laravel. Simple Laravel applications may use the web.php or api.php files to describe their routes. Since they’re small, there is no much to write other routes and is sane to keep it under 60 or less. when adding client shopping-related websites we need to add functionalities, but the need arose to better organize routes in separate files like (admin, affiliate, vendor, customer route file).
Just add another file
Go to your App/Providers/RouteServiceProvider.php
and find the boot() method(Laravel 8). Here the Service Provider will map your Routes.
Example
#App/Providers/RouteServiceProvider.php public function boot() { $this->configureRateLimiting(); $this->routes(function () { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web.php')); /* add affiliate route */ Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/affiliate.php')); }); }
OR
#App/Providers/RouteServiceProvider.php public function boot() { $this->configureRateLimiting(); $this->routes(function () { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); Route::middleware('web') ->namespace($this->namespace) ->group(function ($router) { require base_path('routes/web.php'); /* add affiliate route */ require base_path('routes/affiliate.php'); }); }); }