我估計我們所有人都遇到過這樣的情況,即我們有一個寫滿路由的超大檔案,不騙你,這讓我很長一段時間幾近抓狂,我不得不想個辦法解決這個問題, 因此,這就是我最終用來構造路由檔案的方法,
最初,我想到了利用路由組方法可以接收檔案,這就是 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'));
}
}
我將與用戶有關的路由抽象到了一個名為 users.php 的檔案中,并將 mapApiRoutes 復制為 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'));
}
我知道您在想什么,顯然,這并不是最好的解決方案,因為每當我們需要創建新檔案時,都必須像之前一樣注冊它, 因此,我不得不改進這個最初的想法,
我想到了將整個應用程式中的公共部分拆分成單獨的路由檔案,并且我想到我們的所有路由都不能超出已認證、訪客和公共路由的范圍,
我將路由檔案夾的結構優化成下面這樣:
├── routes │ ├── api │ │ ├── public │ | │ ├── users.php │ │ ├── auth │ | │ ├── users.php │ │ ├── guest │ | │ ├── users.php
乍一看,您可能會認為 “嗯,它并沒有太大變化,我們還是需要去映射這些檔案”, 但是,實際上我們可以利用 php 原生提供的名為 glob 的函式,這是一種開箱即用的解決方案,因為我們沒有與 laravel 的解決方案耦合,
glob 接收一個正則,并且可以在與我們的正則匹配的路徑下找到檔案名, 因此,我們的路由是在特定檔案夾下組織的,我們現在可以在這些檔案夾下找到所有檔案,并將它們注冊到其中間件,
<?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);
}
}
}
現在,無論何時我們創建一個新檔案,foreach 都將找到它,因為它是使用正則匹配(該檔案位于對應的路徑下,并且具有 PHP 擴展名,因此它與我們的正則匹配),簡直太騷了!但是請稍等片刻,
這些檔案將如何注冊?
如果您研究過 laravel 的生命周期,您就知道服務提供者是 laravel 請求的生命周期的一部分,我們可以利用此功能動態注冊我們的路線,
就是這樣!我希望您喜歡它,
譯文地址:https://learnku.com/laravel/t/42989
以上就是一種顆粒度很小的 Laravel 路由檔案劃分方式
更多學習內容請訪問:
騰訊T3-T4標準精品PHP架構師教程目錄大全,只要你看完保證薪資上升一個臺階(持續更新)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/59839.html
標籤:PHP
