Home  >  Article  >  PHP Framework  >  A very fine-grained way to divide Laravel routing files (translation)

A very fine-grained way to divide Laravel routing files (translation)

藏色散人
藏色散人forward
2020-04-09 11:44:502867browse

I guess all of us have encountered a situation where we have a very large file filled with routes. Not gonna lie, this drove me nearly crazy for a long time and I had to figure out a way to fix it. So this is how I ended up constructing the routing file.

Recommended tutorial: "laravel tutorial"

Initially, I thought of using the route group method to receive files. This is how laravel splits the route at RouteServiceProvider.

<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator&#39;s root namespace.
     *
     * @var string
     */
    protected $namespace = &#39;App\Http\Controllers&#39;;
    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //
        parent::boot();
    }
    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();
        $this->mapWebRoutes();
        //
    }
    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware(&#39;web&#39;)
             ->namespace($this->namespace)
             ->group(base_path(&#39;routes/web.php&#39;));
    }
    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix(&#39;api&#39;)
             ->middleware(&#39;api&#39;)
             ->namespace($this->namespace)
             ->group(base_path(&#39;routes/api.php&#39;));
    }
}

I abstracted the user-related routes into a file called users.php and copied mapApiRoutes as mapUsersRoutes and pointed it to my users.php file.

 /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();
        $this->mapWebRoutes();
        $this->mapUsersRoutes();
        //
    }
/**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapUsersRoutes()
    {
        Route::prefix(&#39;api&#39;)
             ->middleware(&#39;api&#39;)
             ->namespace($this->namespace)
             ->group(base_path(&#39;routes/users.php&#39;));
    }

I know what you are thinking, obviously this is not the best solution because every time we need to create a new file we have to register it like before. So I had to refine this initial idea.

I had the idea of ​​splitting the public parts throughout the application into separate routing files, and I had the idea that none of our routes could go beyond authenticated, guest, and public routes.

I optimized the structure of the routing folder to look like this:

├── routes   
│   ├── api    
│   │   ├── public
│   |   │   ├── users.php 
│   │   ├── auth
│   |   │   ├── users.php 
│   │   ├── guest
│   |   │   ├── users.php

At first glance, you might think “Well, it doesn’t change much, we still need to map these files ". However, we can actually leverage a function called glob provided natively by php, which is an out-of-the-box solution since we are not coupled to laravel's solution.

glob accepts a regex and can find filenames under paths that match our regex. So our routes are organized under specific folders and we can now find all files under these folders and register them to their middleware.

<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator&#39;s root namespace.
     *
     * @var string
     */
    protected $namespace = &#39;App\Http\Controllers&#39;;
    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapAuthRoutes();
        $this->mapGuestRoutes();
        $this->mapPublicRoutes();
//        $this->mapWebRoutes();
        //
    }
    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware(&#39;web&#39;)
            ->namespace($this->namespace)
            ->group(base_path(&#39;routes/web.php&#39;));
    }
    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapAuthRoutes()
    {
        foreach (glob(base_path(&#39;routes/api/auth/*.php&#39;)) as $file) {
            Route::prefix(&#39;api&#39;)
                ->middleware([&#39;api&#39;, &#39;auth:api&#39;])
                ->group($file);
        }
    }
    protected function mapGuestRoutes()
    {
        foreach (glob(base_path(&#39;routes/api/guest/*.php&#39;)) as $file) {
            Route::prefix(&#39;api&#39;)
                ->middleware([&#39;api&#39;, &#39;guest:api&#39;])
                ->group($file);
        }
    }
    protected function mapPublicRoutes()
    {
        foreach (glob(base_path(&#39;routes/api/public/*.php&#39;)) as $file) {
            Route::prefix(&#39;api&#39;)
                ->middleware(&#39;api&#39;)
                ->group($file);
        }
    }
}

Now, whenever we create a new file, foreach will find it because it is using regex matching (the file is in the corresponding path and has a PHP extension, so it matches our regular match). It's so sexy! But please wait a moment.

How will these files be registered?

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

That's it! I hope you like it.

Original address: https://dev.to/secmohammed/how-to-separa...

Translation address: https://learnku.com/laravel/t /42989

The above is the detailed content of A very fine-grained way to divide Laravel routing files (translation). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete