未调用Laravel资源控制器方法

未调用Laravel资源控制器方法,laravel,controller,routing,laravel-5.6,Laravel,Controller,Routing,Laravel 5.6,我有控制器LeadsController、LeadsAttributesController和LeadsReminderController 当我转到/leads/attributes时,框架应该调用index函数,但是在这种情况下,会出现一个空白页,并且不会显示任何异常。但是,到/leads/attributes/create的路径应该可以正常工作 如果按以下方式重新构造路由,则路由工作正常 Route::prefix('leads')->group(function(){ Ro

我有控制器LeadsController、LeadsAttributesController和LeadsReminderController

当我转到/leads/attributes时,框架应该调用index函数,但是在这种情况下,会出现一个空白页,并且不会显示任何异常。但是,到/leads/attributes/create的路径应该可以正常工作

如果按以下方式重新构造路由,则路由工作正常

Route::prefix('leads')->group(function(){
    Route::resource('attributes' , 'LeadAttributesController' , ['as' => 'leads']);
    Route::resource('reminders' , 'LeadRemindersController' , ['as' => 'leads']);
});
Route::resource('leads' , 'LeadsController');

有人能解释一下框架的这种行为吗?

解决这个问题的方法是将LeadsController路由放在组路由下:

Route::prefix('leads')->group(function(){
    Route::resource('attributes' , 'LeadAttributesController' , ['as' => 'leads']);
    Route::resource('reminders' , 'LeadRemindersController' , ['as' => 'leads']);
});
Route::resource('leads' , 'LeadsController');

您必须这样做的原因是,leads show路由的通配符将默认接受任何内容。当laravel收到请求时,它将尝试将其与第一条路径匹配,因为您的Lead资源位于嵌套资源之上,因此它将与Lead的显示方法匹配,而不是与正确的嵌套资源匹配。

您是否可以转到.env文件并将APP_DEBUG设置为true,然后重试。您将得到确切的错误。您的“leads/attributes”uri最终会调用LeadController@show'所以最好用小前缀替换前缀different@HassanRaza我正在使用开发环境,调试选项设置为true@Purvesh你说得对。谢谢,但我需要知道原因。问题中已经提到了这一点。@BirendraGurung我已经在我的回答中添加了一个解释,如果你仍然坚持,请告诉我。
Route::prefix('leads')->group(function(){
    Route::resource('attributes' , 'LeadAttributesController' , ['as' => 'leads']);
    Route::resource('reminders' , 'LeadRemindersController' , ['as' => 'leads']);
});
Route::resource('leads' , 'LeadsController');