Laravel's routing is powerful, routing rules are defined by default in routes.php files, but as the project grows, we need to define more and more rules, and if hundreds of thousands of routes are defined in one file, how do you maintain them? If different people are defining routes in the same file, this creates a conflict, so it is necessary to split the routes.php file into multiple files, which can be divided according to the functional modules, and here is an elegant way to do this.
The app/providers/routeserviceprovider.php map 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);
}
});
}
Folder organization structure as shown:
This allows it to traverse the files in the App/http/routes/folder, traversing the map method of each file, each of which has a similar structure, for example:
<?php
namespace App\http\routes;
Use Illuminate\contracts\routing\registrar;
Class Homeroutes
{
Public Function Map (registrar $router)
{
$router->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 ']);
});
}
}
By dividing the routing rules into different files, the routing files can be managed separately according to the functional modules. In addition, you can also simple segmentation, directly to the definition of routes.php into multiple files, through the require way to introduce, 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:
PHP Artisan Route:cache
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.