Laravel the best way to split routes.php routing files

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.