我定义了以下路由:
Route::resource('profile', 'ProfileController', ['except' => ['create', 'destroy']]);
但是,当我尝试使用以下命令重定向到profile/{id}方法时:
redirect()->route('profile', [$userId]);
我得到以下错误:
InvalidArgumentException in UrlGenerator.php line 278:
Route [profile] not defined.
有什么问题吗?
route方法接受路由名作为第一个参数。所有特定的资源路由都有自己的名字,但是没有一个是用基本资源(profile)的名字创建的。
通过运行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]);
尝试重定向
return redirect('profile/2');