Talking about a method of dividing laravel routing files

Time:2022-6-16

At first, I thought of using the routing group method to receive files. This is how laravel splits routes at the routeserviceprovider.


<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider{
    protected $namespace = 'App\Http\Controllers';
    public function boot(){
        parent::boot();
    }
    public function map(){
        $this->mapApiRoutes();
        $this->mapWebRoutes();
    }
    protected function mapWebRoutes(){
        Route::middleware('web')
        ->namespace($this->namespace)
        ->group(base_path('routes/web.php'));
    }
    protected function mapApiRoutes(){
        Route::prefix('api')
        ->middleware('api')
        ->namespace($this->namespace)
        ->group(base_path('routes/api.php'));
    }
}

I abstracted the user – related route to a named users PHP, and copy mapapiroutes as mapusersroutes and point to my users PHP file.


public function map(){
    $this->mapApiRoutes();
    $this->mapWebRoutes();
    $this->mapUsersRoutes();
}
protected function mapUsersRoutes(){
    Route::prefix('api')
    ->middleware('api')
    ->namespace($this->namespace)
    ->group(base_path('routes/users.php'));
}

I know what you are thinking. Obviously, this is not the best solution, because whenever we need to create a new file, we must register it as before. Therefore, I have to improve the original idea.

I thought of splitting the public part of the entire application into separate routing files, and I thought that all our routes could not go beyond the scope of authenticated, guest and public routes.

I optimized the structure of the routing folder as follows:

├── routes  

│   ├── api   

│   │   ├── public

│   |   │   ├── users.php

│   │   ├── auth

│   |   │   ├── users.php

│   │   ├── guest

│   |   │   ├── users.php

At first glance, you might think, “well, it hasn’t changed much. We still need to map these files.”. However, in fact, we can use the function named glob provided by PHP natively, which is an out of the box solution, because we are not coupled with laravel’s solution.

Glob receives a regular and can find the file name in the path that matches our regular. Therefore, our routing is organized under specific folders. We can now find all the files under these folders and register them with the middleware.


<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider{
    protected $namespace = 'App\Http\Controllers';
    public function map(){
        $this->mapAuthRoutes();
        $this->mapGuestRoutes();
        $this->mapPublicRoutes();
    }
    protected function mapWebRoutes(){
        Route::middleware('web')
        ->namespace($this->namespace)
        ->group(base_path('routes/web.php'));
    }
    protected function mapAuthRoutes(){
        foreach (glob(base_path('routes/api/auth/*.php')) as $file) {
            Route::prefix('api')
            ->middleware(['api', 'auth:api'])
            ->group($file);
        }
    }
    protected function mapGuestRoutes(){
        foreach (glob(base_path('routes/api/guest/*.php')) as $file) {
            Route::prefix('api')
            ->middleware(['api', 'guest:api'])
            ->group($file);
        }
    }
    protected function mapPublicRoutes(){
        foreach (glob(base_path('routes/api/public/*.php')) as $file) {
            Route::prefix('api')
            ->middleware('api')
            ->group($file);
        }
    }
}

Now, whenever we create a new file, foreach will find it because it uses regular matching (the file is under the corresponding path and has a PHP extension, so it matches our regular matching). It’s so coquettish! But please wait a moment.

How will these files be registered?

If you have studied the life cycle of laravel, you will know that service providers are part of the life cycle of laravel requests. We can use this function to dynamically register our routes.

The above is about the details of a laravel routing file division method. For more information about a laravel routing file division method, please pay attention to other developeppaer related articles!

Recommended Today

[basics] webpack5 build vue3+typescript basic development environment

preface Recently, we upgraded the company’s ancient projects and found that the performance of webpack5 has been greatly improved. In addition to the use of vue3, we specially tried to build a vue3 development environment from scratch. The main purpose of not using the official scaffold is to deepen our understanding of this technology Warehouse […]