我定义了以下路线:
Route::resource('profile', 'ProfileController', ['except' => ['create', 'destroy']]);
但是,当我尝试使用以下命令重定向到 profile/{id} 方法时:
redirect()->route('profile', [$userId]);
我收到以下错误:
InvalidArgumentException in UrlGenerator.php line 278:
Route [profile] not defined.
可能是什么问题?
最佳答案
route 方法将路由名称作为第一个参数。所有特定的资源路由都将获得自己的名称,但是没有任何路由是使用基础资源(配置文件)的名称创建的。
通过运行php artisan route:list
,您将看到所有路由及其名称的列表。对于您来说,它应该看起来像这样:
+--------+----------+-----------------------------------------+-----------------+---------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-----------------------------------------+-----------------+---------------------------------------------------+------------+
| | POST | profile | profile.store | App\Http\Controllers\ProfileController@store | |
| | GET|HEAD | profile | profile.index | App\Http\Controllers\ProfileController@index | |
| | GET|HEAD | profile/create | profile.create | App\Http\Controllers\ProfileController@create | |
| | PATCH | profile/{profile} | | App\Http\Controllers\ProfileController@update | |
| | PUT | profile/{profile} | profile.update | App\Http\Controllers\ProfileController@update | |
| | DELETE | profile/{profile} | profile.destroy | App\Http\Controllers\ProfileController@destroy | |
| | GET|HEAD | profile/{profile} | profile.show | App\Http\Controllers\ProfileController@show | |
| | GET|HEAD | profile/{profile}/edit | profile.edit | App\Http\Controllers\ProfileController@edit | |
+--------+----------+-----------------------------------------+-----------------+---------------------------------------------------+------------+
因此,既然您似乎打算显示用户个人资料,那么这应该是您想要做的:
redirect()->route('profile.show', [$userId]);
关于php - 重定向到资源路由时路由不存在 [Laravel],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32034031/