Objective
Laravel's routing is powerful, the default is defined in the routes.php file, as the project grows larger, we need to define more and more routes, imagine if hundreds of thousands of routes are defined in a file, how to maintain? There may be different people who define routes in the same file, which creates a conflict, so we need to split the routes.php file.
Here's an elegant way to do this.
In app/Providers/RouteServiceProvider.php the map method you can define the following:
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 document organization chart is as follows:
This will traverse the app/Http/Routes/ files in the folder, traverse each file routing class map method, each file structure is similar,
As an example:
<?php/** * Created by Phpstorm.
* USER:XL * DATE:2016/7/4 * time:18:07 * * namespace app\http\routes;
Use Illuminate\contracts\routing\registrar; Class Homeroutes {Public function Map (registrar $router) {$router->group (' domain ' => ' www.tanteng.me ', ' m
Iddleware ' => ' 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 ']);
}); }
}
The routing rules are written to each file in the map method, so that a good routes.php file separation management. In addition, you can also simple segmentation, directly to the definition of routes.php into multiple files, through require the way introduced, but which is better, at a glance.
So does this route to separate multiple files is not to increase the number of calls, will not affect performance? The answer is no need to worry. Through the laravel command:
After the routing cache file is generated, the route only reads the routing rules of the cached files, so it does not affect performance, which makes development more efficient and normative.
OK, the above is Laravel routing file (routes.php) The best way to split the whole content, I hope to learn laravel help. We also hope that we can support the cloud-dwelling community.