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

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

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),和碩企業(yè)網(wǎng)站建設(shè),和碩品牌網(wǎng)站建設(shè),網(wǎng)站定制,和碩網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,和碩網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

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é)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

網(wǎng)站題目:Laravel路由文件劃分方式的示例分析
文章轉(zhuǎn)載:http://bm7419.com/article44/psspee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、品牌網(wǎng)站設(shè)計網(wǎng)站內(nèi)鏈、建站公司域名注冊、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化