這篇文章將為大家詳細講解有關Laravel 路由文件劃分方式的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
Laravel 是一套簡潔、優雅的PHP Web開發框架。它可以讓你從面條一樣雜亂的代碼中解脫出來;它可以幫你構建一個完美的網絡APP,而且每行代碼都可以簡潔、富于表達力。
我估計我們所有人都遇到過這樣的情況,即我們有一個寫滿路由的超大文件。不騙你,這讓我很長一段時間幾近抓狂,我不得不想個辦法解決這個問題。 因此,這就是我最終用來構造路由文件的方法。
最初,我想到了利用路由組方法可以接收文件,這就是 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 路由文件劃分方式的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。