我估計我們所有人都遇到過這樣的情況,即我們有一個寫滿路由的超大檔案。不騙你,這讓我很長一段時間幾近抓狂,我不得不想個辦法解決這個問題。因此,這就是我最終用來構造路由文件的方法。
推薦教學:《laravel教學》
最初,我想到了利用路由組方法可以接收文件,這就是 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://dev.to/secmohammed/how-to-separa...
翻譯網址:https://learnku.com/laravel/t /42989
以上是一種顆粒度很小的 Laravel 路由檔案劃分方式(翻譯)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

若尋找Laravel替代品,Node.jswithExpress.js、Django、RubyonRails和ASP.NETCore都是可選方案。 1.Node.jswithExpress.js適合需要高性能和擴展性的項目。 2.Django適用於需要快速開發和全功能的項目。 3.RubyonRails適合快速原型和靈活開發。 4.ASP.NETCore適合高流量和跨平台開發,但學習曲線較陡。

theKeyChallenGesinManagingDistributedTeamSareCommunicationGaps,TimeZonEdifferences,andTaskManagement.projectManagementToolShelPoverComethesechallengesby:1)增強CommunicationThrancyThrouncyThrouncyThroughthroughplatformslikeslikeSlikeSlikeSlackandMicrosoftTeams,2)ManagingTimeZonEdingiffererenses

領導遠程團隊的關鍵在於使用技術、建立信任和製定個性化策略。 1)利用通信工具和任務管理系統確保任務分配和狀態更新清晰。 2)通過異步溝通避免倦怠,增強生產力。 3)通過授權和設定明確目標,激勵團隊成員。 4)關注團隊滿意度和協作,定期進行全面檢查。

確保分佈式團隊成員公平獲取工具和資源的方法包括:1)使用低帶寬替代方案,如異步視頻或文本更新,解決連接問題;2)設立核心重疊工作時間,並提供靈活工作時間,管理時區差異;3)通過翻譯功能和文化意識培訓,適應不同文化需求。這些策略有助於創建一個包容和高效的遠程工作環境。

ForenHancingRemoteCollaboration,AninStantMessagingToolMusThave:1)可靠性ForConsistentMessageDelivery,2)AnintuiveduserInterInterInterterfaceForeasyNavigation,3)Real-Timenotificationstostostostostostostostostostostostostostostostostostayupdated,4)SeamelesselessfileSlessFileSlessFileSlessFileSlesselessFileSleSlessForefliceForefliceDocumentExchange,5)集成

Thebiggestchallengeofmanagingdistributedteamsiscommunication.Toaddressthis,usetoolslikeSlack,Zoom,andGitHub;setclearexpectations;fostertrustandautonomy;implementasynchronousworkpatterns;andintegratetaskmanagementwithcommunicationplatformsforefficient

Laravel的最新版本在安全性方面有显著提升,包括:1.增强的CSRF保护,通过更robust的token验证机制;2.改进的SQL注入防护,通过增强的查询构建方法;3.更好的会话加密,确保用户数据安全;4.改进的认证系统,支持更细粒度的用户认证和多因素认证(MFA)的实现。

TonavigatesChedulingConflictSinaglobalworkforce,Usetechnology,Ensathy and Strategicplanning:1)hosporlikeTimeBuddyorCalendlyForscheduling; 2)RotateMeetingTimeStoEnsurefairness; 3)spentCoreSurefair; 3)specoreCoreHoursibible foreverlap; 4)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版
中文版,非常好用

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),