我估計我們所有人都遇到過這樣的情況,即我們有一個寫滿路由的超大文件。不騙你,這讓我很長一段時間幾近抓狂,我不得不想個辦法解決這個問題。 因此,這就是我最終用來構造路由文件的方法。
推薦教程:《laravel教程》
最初,我想到了利用路由組方法可以接收文件,這就是 laravel 在 RouteServiceProvider 處拆分路由的方式。
<?php namespace AppProviders; use IlluminateFoundationSupportProvidersRouteServiceProvider as ServiceProvider; use IlluminateSupportFacadesRoute; 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 = 'AppHttpControllers'; /** * 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 AppProviders; use IlluminateFoundationSupportProvidersRouteServiceProvider as ServiceProvider; use IlluminateSupportFacadesRoute; 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 = 'AppHttpControllers'; /** * 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://dev.to/secmohammed/how-to-separa…譯文地址:https://learnku.com/laravel/t/42989