This article is a best tutorial on Laravel splitting routing File (routes. php). the content of this article is very detailed. For more information about Laravel, see. This article is a best tutorial on Laravel splitting routing File (routes. php). the content of this article is very detailed. For more information about Laravel, see.
Preface
Laravel has powerful routing functions, which are defined by default inRoutes. phpIn the file, as the project grows, we need to define more and more routes. imagine how to maintain if hundreds of thousands of routes are defined in one file? Different people may define routes in the same file, which leads to conflicts. Therefore, we need to separate them.Routes. phpFile.
The following describes an elegant method.
Inapp/Providers/RouteServiceProvider.php
Ofmap
The method can be defined as follows:
public function map(Router $router){ $router->group(['namespace' => $this->namespace], function ($router) { //require app_path('Http/routes.php'); foreach (glob(app_path('Http//Routes') . '/*.php') as $file) { $this->app->make('App\\Http\\Routes\\' . basename($file, '.php'))->map($router); } });}
The file structure is as follows:
In this way, it will traverseapp/Http/Routes/
Folder to traversemap
METHOD. The structure of each file is similar,
For example:
group(['domain' => 'www.tanteng.me', 'middleware' => 'web'], function ($router) { $router->auth(); $router->get('/', ['as' => 'home', 'uses' => 'IndexController@index']); $router->get('/blog', ['as' => 'index.blog', 'uses' => 'BlogController@index']); $router->get('/resume', ['as' => 'index.resume', 'uses' => 'IndexController@resume']); $router->get('/post', ['name' => 'post.show', 'uses' => 'ArticleController@show']); $router->get('/contact', ['as' => 'index.contact', 'uses' => 'IndexController@contact']); $router->post('/contact/comment', ['uses' => 'IndexController@postComment']); $router->get('/travel', ['as' => 'index.travel', 'uses' => 'TravelController@index']); $router->get('/travel/latest', ['as' => 'travel.latest', 'uses' => 'TravelController@latest']); $router->get('/travel/{destination}/list', ['as' => 'travel.destination', 'uses' => 'TravelController@travelList']); $router->get('/travel/{slug}', ['uses' => 'TravelController@travelDetail']); $router->get('/sitemap.xml', ['as' => 'index.sitemap', 'uses' => 'IndexController@sitemap']); }); }}
Write the routing rules tomap
Method.Routes. phpSeparate file management. In addition, you can simply separateRoutes. phpIs split into multiple files throughrequire
But which one is better and clear at a glance.
In this way, after the route is separated from multiple files, will it increase the number of calls and affect the performance? The answer is don't worry. PassLaravelCommand:
php artisan route:cache
After a route cache file is generated, the route will only read the routing rules of the cached file, so it will not affect the performance. This will make development more efficient and standardized.
Okay, the above is the Laravel routing file (Routes. php) It is the best way to split all the content, hoping to help you learn Laravel. I also hope that you will support PHP.
For more information about how Laravel best splits route files (routes. php), see PHP!