Best way to split routes.php routes file in Laravel

Laravel's routing function is very powerful. By default, routing rules are defined in the routes.php file. However, as the project grows, we need to define more and more rules. If hundreds of thousands of routes are defined in one file, how to maintain it? If different people define routes in the same file, this will cause conflicts. Therefore, it is necessary for us to divide the routes.php file into multiple files, which can be divided according to functional modules. The following is a very elegant way.

The map method in app/Providers/RouteServiceProvider.php 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 folder organization structure is as follows:

In this way, it will traverse the files under the app/Http/Routes/ folder, and traverse the map method of each file, where the structure of each file is similar, 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 and writing routing rules into different files, routing files can be managed separately according to function modules. In addition, you can also simply split, directly split the definitions in routes.php into multiple files, and import them through require, but which one is better is clear at a glance.

So after this route separates multiple files, doesn't it increase the number of calls, and will it affect performance? The answer is not to worry. Via Laravel's command:

php artisan route:cache

After the routing cache file is generated, the routing will only read the routing rules of the cache file, so it will not affect the performance, which makes the development more efficient and standardized.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326185768&siteId=291194637