声明路由会覆盖另一个路由



当使用参数声明路由时,Laravel以某种方式忽略了紧随其后声明的任何其他路由(任何具有名称命名约定的路由(。

这是我routes.php文件

Route::group(['middleware' => ['auth','member'], 'prefix' => 'profile'], function () {
Route::get('/details/{profile_id}','MemberProfileController@index');
Route::get('/details/image','MemberProfileImageController@index');
Route::get('/details/details','MemberProfileDetailsController@index');
});

当尝试拉profile image时,我一直Requested resource could not be found,但是如果我在/details/{profile_id}之前声明/details/image,那么它就可以了。

难道是拉拉维尔将{profile_id}image相匹配?我在 Laravel 文档中找不到任何说明情况会如此的内容。

我做错了什么吗?

您必须以这种方式安排路线。 因为/details/image将被视为/details/*.

Route::group(['middleware' => ['auth','member'], 'prefix' => 'profile'], function () {
Route::get('/details/image','MemberProfileImageController@index');
Route::get('/details/details','MemberProfileDetailsController@index');
Route::get('/details/{profile_id}','MemberProfileController@index');
});

或者您也可以使用过滤器来获取确切的值

Route::get('/details/{profile_id}','MemberProfileController@index')->where('profile_id', '[0-9]+');

您需要除使用此路由public function endix之外Route::get('/details/image','MemberProfileImageController@index');

在你的控制器上放这个

public function__construct(){
{$this->middleware('auth', ['except' => ['index']]);
}

或者你可以改变它,因为索引是一个独特的函数。

最新更新