问题
I have recently installed laravel 6 for a project.
unfortunately, the routes don't work properly.
for example i lead a user to a page with this route..
{{ route('user.edit', ['id' => $user->id]) }}.
It should go to admin/user/{user}/edit
.
But it goes to an unknown route like this:
/admin/admin/user//edit?id=1
my route :
Route::group(['namespace' => 'Admin', 'middleware' => ['auth', 'IsVerified'], 'prefix' => 'admin'], function () {
route::resource('/user', 'UserController');
});
回答1:
Your route model binding is incorrect, change your href to this
{{ route('user.edit', ['user' => $user]) }}
Result:
/admin/user/1/edit
Calling Route::resource
on a model returns this url for an edit route
+--------+-----------+----------------------------+------------------+------------------------------------------------------------------------+-------------------------------------------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+----------------------------+------------------+------------------------------------------------------------------------+-------------------------------------------------+
| | GET|HEAD | admin/user/{user}/edit | user.edit | App\Http\Controllers\Admin\UserController@edit | web,auth,IsVerified |
Note that the user
model is used for the binding and then the ID is automatically fetched by the getRouteKeyName
function which returns id
by default
回答2:
- The default placeholder for a resource is the model 'user' not 'id'
{{ route('user.edit', ['id' => $user->id]) }}
would become:
{{ route('user.edit', $user) }}
- The double slash in your route is caused by the unecessary slash in
route::resource('/user', 'UserController');
which could just be:
route::resource('user', 'UserController');
- As for the double 'admin', perhaps that is being output in your view, as your namespace and grouping appears correct.
来源:https://stackoverflow.com/questions/58136586/how-to-prefix-admin-to-routes-in-laravel