此資源定義好: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。有什麼辦法解決這個問題嗎?
要聲明資源路由,您需要將資源名稱,即「servers」傳遞給資源方法。 –
爲什麼你想把它包裝成組?從我可以看到它已經在一個組中。 –
@TheAlpha是的,但在這種情況下,我不能使用前綴。我已經檢查了核心,並且有一個'prefixedResource'方法,所以我希望如果資源的名字只是一個'/',它可以使用前綴作爲名稱。但似乎不是。順帶一提,謝謝你的回答。 – MrRP