Laravel the best way to split routing files (routes.php) _php instances

Source: Internet
Author: User

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:

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.

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.

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.