Home > PHP Framework > Laravel > body text

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

藏色散人
Release: 2020-04-09 11:44:50
forward
2969 people have browsed it

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;));
    }
}
Copy after login

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;));
    }
Copy after login

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
Copy after login

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);
        }
    }
}
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template