2017-02-14 43 views
3

我使用Laravel 5.4更好的方式來在同一組添加路由類

路線服務提供商的代碼如下

Route::prefix('api/v1') 
    ->middleware('api') 
    ->namespace($this->namespace) 
    ->group(base_path('routes/Login.php')); 

Route::prefix('api/v1') 
    ->middleware('api') 
    ->namespace($this->namespace) 
    ->group(base_path('routes/Register.php')); 

有沒有辦法寫在同一組中的兩個路徑文件?

這樣的事情...

Route::prefix('api/v1') 
    ->middleware('api') 
    ->namespace($this->namespace) 
    ->group(base_path('routes/Login.php')) 
    ->group(base_path('routes/Register.php')); 

在5.3。我們可以這樣寫...

Route::group([ 
    'middleware' => 'auth:api', 
    'namespace'  => $this->namespace, 
    'prefix'  => 'api/v1', 
], function ($router) { 
    require base_path('routes/API/Driver/Driver.php'); 
    require base_path('routes/API/Vehicle/Vehicle.php'); 
}); 

回答

1

group方法使用相同的解決方案,在5.3帶一個函數作爲參數,所以它應該只是

Route::prefix('api/v1') 
    ->middleware('api') 
    ->namespace($this->namespace) 
    ->group(function ($router) { 
     require base_path('routes/API/Driver/Driver.php'); 
     require base_path('routes/API/Vehicle/Vehicle.php'); 
    }); 


// routes/API/Driver/Driver.php 
<?php 

Route::get('/drivers', '[email protected]'); 
// (Route URI is '/api/v1/drivers'); 
+0

我想,你誤解了我的問題....我試圖在同一組下添加多個php文件。不是同一組下的多條路線。 – Pankaj

+0

@Pankaj是的,我不認爲我得到你想要的東西...你能展示一個這樣的PHP文件的例子嗎? – alepeino

+0

每個php文件都有它們的路線。 – Pankaj

2

可以在5.4

Route::group(['prefix' => 'api', /* .... */], function() { 

     // extract this to external files as needed 
     Route::get('/path', '[email protected]'); 

    }); 
+0

請在我的問題中查看最新的5.4格式提及。 – Pankaj

+0

group()方法不返回任何東西。所以你不能鏈接它。但亞歷杭德羅說你可以通過關閉。 –

相關問題