Saturday, April 15, 2017

Laravel 5.4 route group and resource mess up the parameters

This resource definition is good:

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

Gives:

+--------+-----------+-----------------------+-----------------+-----------------------------------------------+--------------+
| 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          |
+--------+-----------+-----------------------+-----------------+-----------------------------------------------+--------------+

But I need to wrap it into a group with prefix like this:

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

And here comes the problem, because of prefix the parameterised routes' parameters are empty:

+--------+-----------+-----------------+-----------------+-----------------------------------------------+--------------+
| 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          |
+--------+-----------+-----------------+-----------------+-----------------------------------------------+--------------+

So the servers/1 goes 404. Is there any way to fix this problem?



via MrRP

Advertisement