溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Laravel 路由文件劃分方式的示例分析

發(fā)布時間:2021-04-25 11:35:17 來源:億速云 閱讀:138 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)Laravel 路由文件劃分方式的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

Laravel 是什么

Laravel 是一套簡潔、優(yōu)雅的PHP Web開發(fā)框架。它可以讓你從面條一樣雜亂的代碼中解脫出來;它可以幫你構(gòu)建一個完美的網(wǎng)絡(luò)APP,而且每行代碼都可以簡潔、富于表達(dá)力。

我估計我們所有人都遇到過這樣的情況,即我們有一個寫滿路由的超大文件。不騙你,這讓我很長一段時間幾近抓狂,我不得不想個辦法解決這個問題。 因此,這就是我最終用來構(gòu)造路由文件的方法。

最初,我想到了利用路由組方法可以接收文件,這就是 laravel 在 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's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';
    /**
     * 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('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }
    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }
}

我將與用戶有關(guān)的路由抽象到了一個名為 users.php 的文件中,并將 mapApiRoutes 復(fù)制為 mapUsersRoutes 并指向到我的 users.php 文件。

 /**
     * 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('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/users.php'));
    }

我知道您在想什么,顯然,這并不是最好的解決方案,因為每當(dāng)我們需要創(chuàng)建新文件時,都必須像之前一樣注冊它。 因此,我不得不改進(jìn)這個最初的想法。

我想到了將整個應(yīng)用程序中的公共部分拆分成單獨的路由文件,并且我想到我們的所有路由都不能超出已認(rèn)證、訪客和公共路由的范圍。

我將路由文件夾的結(jié)構(gòu)優(yōu)化成下面這樣:

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

乍一看,您可能會認(rèn)為 “嗯,它并沒有太大變化,我們還是需要去映射這些文件”。 但是,實際上我們可以利用 php 原生提供的名為 glob 的函數(shù),這是一種開箱即用的解決方案,因為我們沒有與 laravel 的解決方案耦合。

glob 接收一個正則,并且可以在與我們的正則匹配的路徑下找到文件名。 因此,我們的路由是在特定文件夾下組織的,我們現(xiàn)在可以在這些文件夾下找到所有文件,并將它們注冊到其中間件。

<?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's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';
    /**
     * 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('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    }
    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    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);
        }
    }
}

現(xiàn)在,無論何時我們創(chuàng)建一個新文件,foreach 都將找到它,因為它是使用正則匹配(該文件位于對應(yīng)的路徑下,并且具有 PHP 擴(kuò)展名,因此它與我們的正則匹配)。簡直太騷了!但是請稍等片刻。

關(guān)于“Laravel 路由文件劃分方式的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI