此资源定义很好:
Route::resource('servers', 'ServerController');
给出:
+--------+-----------+-----------------------+-----------------+-----------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-----------------------+-----------------+-----------------------------------------------+--------------+
| | GET|HEAD | servers | servers.index | App\Http\Controllers\ServerController@index | web |
| | POST | servers | servers.store | App\Http\Controllers\ServerController@store | web |
| | GET|HEAD | servers/create | servers.create | App\Http\Controllers\ServerController@create | web |
| | GET|HEAD | servers/{server} | servers.show | App\Http\Controllers\ServerController@show | web |
| | PUT|PATCH | servers/{server} | servers.update | App\Http\Controllers\ServerController@update | web |
| | DELETE | servers/{server} | servers.destroy | App\Http\Controllers\ServerController@destroy | web |
| | GET|HEAD | servers/{server}/edit | servers.edit | App\Http\Controllers\ServerController@edit | 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\ServerController@index | web |
| | POST | servers | servers.store | App\Http\Controllers\ServerController@store | web |
| | GET|HEAD | servers/create | servers.create | App\Http\Controllers\ServerController@create | web |
| | GET|HEAD | servers/{} | servers.show | App\Http\Controllers\ServerController@show | web |
| | PUT|PATCH | servers/{} | servers.update | App\Http\Controllers\ServerController@update | web |
| | DELETE | servers/{} | servers.destroy | App\Http\Controllers\ServerController@destroy | web |
| | GET|HEAD | servers/{}/edit | servers.edit | App\Http\Controllers\ServerController@edit | web |
+--------+-----------+-----------------+-----------------+-----------------------------------------------+--------------+
所以servers/1
去了404.有没有办法解决这个问题?
答案 0 :(得分:2)
您可以使用方法parameters
自定义route中的所有参数。
示例:
Route::prefix('teams')->group(function () {
Route::resource('', 'TeamController')->parameters(['' => 'team']);
});
答案 1 :(得分:1)
根据您的评论:
您希望将命名空间和中间件添加到资源路由中。您可以通过在资源控制器的第三个参数中添加middleware
和namespace
键来实现此目的。
Route::resource('servers', 'ServerController', ['middleware' => 'xyz', 'namespace' => 'xyz']);
修改强>
如果命名空间不起作用,您可以为控制器分配命名空间。
Route::resource('servers', 'Xyz\ServerController', ['middleware' => 'xyz']);