Organization of routes in Laravel

Hello, Habr. Recently, I agreed to a review of a site ordered on freelance. I expected to see controllers that do everything and occupy 200+ lines (and other manifestations of bad code), but everything was pretty decent. Validation did not lie in the controller, which is common enough. Sometimes, of course, there were some PSR-2 inconsistencies, but everything looked fine until I looked into the file with the routes. It occupied several screens and had comments about route groups. I contacted the developer and he said that everything had โ€œgrownโ€ a bit and agreed to fix it. In the next version, I saw that he created several classes with static methods, into which he transferred the code and called them, as is usually done, with authorization. Then I remembered that I had already encountered this and met such an argument: "The documentation does not say anything about taking routes to different files." Just a couple of minutes later I found an article and sent it to the developer. After half an hour, I received the routes in normal form and decided that this article would be useful for many beginners. So this free translation was born.



If you have never edited the RouteServiceProvider file, welcome to cat.



If you are working with a small project and you have only a couple of routes, then there will be no problems. But when the number of routes is too large, working in one file becomes difficult, especially when you have pages for different users, administrators, etc.



Initially, laravel creates 4 files:





Suppose you plan to create a project with ten pages for each type of user:





Create two directories inside routes :











We move api.php from routes to the routes / api and web.php directory in routes / web , and leave the remaining console.php and channels.php in routes.



Create the admin.php file inside routes / web . This file will contain all of our web routes associated with the administrator, and then create user.php there, for user-related routes.











app / Providers / RouteServiceProvider.php - this file is responsible for downloading all the routes of our application. The map () method calls the mapApiRoutes () and mapWebRoutes () methods to load the web.php and api.php files that we have already moved, so let's fix the paths to the route files.



protected function mapWebRoutes() { Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web/web.php')); }
      
      





 protected function mapApiRoutes() { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api/api.php')); }
      
      





Now create new methods for routes / web / admin.php and routes / web / user.php inside RouteServiceProvider.php



 protected function mapAdminWebRoutes() { Route::middleware('web') ->namespace($this->namespace) ->prefix('admin') ->group(base_path('routes/web/admin.php')); }
      
      





 protected function mapUserWebRoutes() { Route::middleware('web') ->namespace($this->namespace) ->prefix('user') ->group(base_path('routes/web/user.php')); }
      
      





Please note that in this code it is possible to add namespace, middleware, prefix, etc. for paths.



Next, just call them from map ():



 public function map() { $this->mapApiRoutes(); $this->mapWebRoutes(); $this->mapAdminWebRoutes(); $this->mapUserWebRoutes(); }
      
      





Last step:



Open routes / web / user.php and add a test route:



 Route::get('/test', function () { return response(' ', 200); });
      
      





Go to site.local / user / test, you should see the text โ€œTest Routeโ€.



All Articles