2017-04-15 66 views
2

此資源定義好:Laravel 5.4路由組和資源陷入困境的參數

Route::resource('servers', 'ServerController'); 

給出:

+--------+-----------+-----------------------+-----------------+-----------------------------------------------+--------------+ 
| Domain | Method | URI     | Name   | Action          | Middleware | 
+--------+-----------+-----------------------+-----------------+-----------------------------------------------+--------------+ 
|  | GET|HEAD | servers    | servers.index | App\Http\Controllers\[email protected] | web   | 
|  | POST  | servers    | servers.store | App\Http\Controllers\[email protected] | web   | 
|  | GET|HEAD | servers/create  | servers.create | App\Http\Controllers\[email protected] | web   | 
|  | GET|HEAD | servers/{server}  | servers.show | App\Http\Controllers\[email protected] | web   | 
|  | PUT|PATCH | servers/{server}  | servers.update | App\Http\Controllers\[email protected] | web   | 
|  | DELETE | servers/{server}  | servers.destroy | App\Http\Controllers\[email protected] | web   | 
|  | GET|HEAD | servers/{server}/edit | servers.edit | App\Http\Controllers\[email protected] | web   | 
+--------+-----------+-----------------------+-----------------+-----------------------------------------------+--------------+ 

但我需要將它與這樣的前綴包裹成團:

Route::group(['prefix' => 'servers'], function() 
{ 
    Route::resource('/', 'ServerController', ['names' => 'servers']); 
}); 

問題出在這裏,因爲前綴參數化路線的參數是空的:

+--------+-----------+-----------------+-----------------+-----------------------------------------------+--------------+ 
| Domain | Method | URI    | Name   | Action          | Middleware | 
+--------+-----------+-----------------+-----------------+-----------------------------------------------+--------------+ 
|  | GET|HEAD | servers   | servers.index | App\Http\Controllers\[email protected] | web   | 
|  | POST  | servers   | servers.store | App\Http\Controllers\[email protected] | web   | 
|  | GET|HEAD | servers/create | servers.create | App\Http\Controllers\[email protected] | web   | 
|  | GET|HEAD | servers/{}  | servers.show | App\Http\Controllers\[email protected] | web   | 
|  | PUT|PATCH | servers/{}  | servers.update | App\Http\Controllers\[email protected] | web   | 
|  | DELETE | servers/{}  | servers.destroy | App\Http\Controllers\[email protected] | web   | 
|  | GET|HEAD | servers/{}/edit | servers.edit | App\Http\Controllers\[email protected] | web   | 
+--------+-----------+-----------------+-----------------+-----------------------------------------------+--------------+ 

所以servers/1去404。有什麼辦法解決這個問題嗎?

+0

要聲明資源路由,您需要將資源名稱,即「servers」傳遞給資源方法。 –

+0

爲什麼你想把它包裝成組?從我可以看到它已經在一個組中。 –

+0

@TheAlpha是的,但在這種情況下,我不能使用前綴。我已經檢查了核心,並且有一個'prefixedResource'方法,所以我希望如果資源的名字只是一個'/',它可以使用前綴作爲名稱。但似乎不是。順帶一提,謝謝你的回答。 – MrRP

回答

1

根據您的意見:

你想要的名稱空間和中間件添加到資源路徑。您可以通過在您的資源控制器的第三個參數中添加middlewarenamespace鍵來實現此目的。

Route::resource('servers', 'ServerController', ['middleware' => 'xyz', 'namespace' => 'xyz']); 

編輯:

如果一個命名空間不工作,你可以指定一個命名空間的控制器。

Route::resource('servers', 'Xyz\ServerController', ['middleware' => 'xyz']); 
+0

我檢查過它,不,名稱空間不能作爲資源參數工作。我已經設置了它,並且仍然給出了默認的命名空間:'App \ Http \ Controllers \ ServerController @ index' – MrRP

+0

@MrRP您可以像這樣實現它:https://laravel.com/docs/5.4/controllers#controllers-and名稱空間 –

+0

@MrRP嘗試使用大寫「Servers」中的「servers」文件夾。 正如PSR-4所述:子目錄名稱必須與子命名空間名稱的大小寫匹配。 –