dongshungai4857 2015-05-29 03:26
浏览 387
已采纳

Laravel 5 Route ::资源在嵌套的Route :: group中

This structure works for (all GET) : /dashboard/video, /dashboard/video/element, /dashboard/video/create,/dashboard/video/element/create, /dashboard/video/element/{id}, but not for /dashboard/video/{id}.

In other words, show($id) isn't getting called for /dashboard/video/{id}.

Any idea why?

Route::group(['prefix' => 'dashboard', 'middleware' => ['csrf', 'auth']], function () {

    Route::group(['prefix' => 'video'], function() {

        // non-standard delete: sends array of ids in request body
        Route::delete('/', 'InteractiveVideoController@destroyMany');
        Route::resource('/', 'InteractiveVideoController');

        Route::resource('element', 'InteractiveElementController');
    });
...
});
  • 写回答

1条回答 默认 最新

  • dongwen5870 2015-05-29 04:15
    关注

    The problem can become easily visible when you execute

    php artisan route:list
    

    From the generated table you can see this:

    |        | GET|HEAD                       | dashboard/video                                       | dashboard.video..index   | App\Http\Controllers\InteractiveVideoController@index      |            |
    |        | GET|HEAD                       | dashboard/video/create                                | dashboard.video..create  | App\Http\Controllers\InteractiveVideoController@create     |            |
    |        | POST                           | dashboard/video                                       | dashboard.video..store   | App\Http\Controllers\InteractiveVideoController@store      |            |
    |        | GET|HEAD                       | dashboard/video/{}                                    | dashboard.video..show    | App\Http\Controllers\InteractiveVideoController@show       |            |
    |        | GET|HEAD                       | dashboard/video/{}/edit                               | dashboard.video..edit    | App\Http\Controllers\InteractiveVideoController@edit       |            |
    |        | PUT                            | dashboard/video/{}                                    | dashboard.video..update  | App\Http\Controllers\InteractiveVideoController@update     |            |
    |        | PATCH                          | dashboard/video/{}                                    |                          | App\Http\Controllers\InteractiveVideoController@update     |            |
    |        | DELETE                         | dashboard/video/{}                                    | dashboard.video..destroy | App\Http\Controllers\InteractiveVideoController@destroy    |            |
    

    As you can see by defining the videos route as / under videos group will not work, simply because in order the resource controller to create restful paths with parameters, it uses the first part of the path as variable name. In your case it is empty (just a slash / that gets stripped), that's why you get this {}.

    You have to change your routes.php and move the videos resource out of the group and it will work, like this:

    Route::group(['prefix' => 'dashboard'], function () {
    
        Route::group(['prefix' => 'video'], function() {
    
            Route::resource('element', 'InteractiveElementController');
        });
    
        // non-standard delete: sends array of ids in request body
        Route::delete('video', 'InteractiveVideoController@destroyMany');
        Route::resource('video', 'InteractiveVideoController');
    
    });
    

    You have to put video resource under video element, otherwise video/element will never match, since the order of the routes that are defined matters.

    This will work for you and now all pages will open normally. You can also check that the routes are defined correctly by executing again:

    php artisan route:list
    
    |        | GET|HEAD                       | dashboard/video                                       | dashboard.video.index   | App\Http\Controllers\InteractiveVideoController@index      |            |
    |        | GET|HEAD                       | dashboard/video/create                                | dashboard.video.create  | App\Http\Controllers\InteractiveVideoController@create     |            |
    |        | POST                           | dashboard/video                                       | dashboard.video.store   | App\Http\Controllers\InteractiveVideoController@store      |            |
    |        | GET|HEAD                       | dashboard/video/{video}                                    | dashboard.video.show    | App\Http\Controllers\InteractiveVideoController@show       |            |
    |        | GET|HEAD                       | dashboard/video/{video}/edit                               | dashboard.video.edit    | App\Http\Controllers\InteractiveVideoController@edit       |            |
    |        | PUT                            | dashboard/video/{video}                                    | dashboard.video.update  | App\Http\Controllers\InteractiveVideoController@update     |            |
    |        | PATCH                          | dashboard/video/{video}                                    |                          | App\Http\Controllers\InteractiveVideoController@update     |            |
    |        | DELETE                         | dashboard/video/{video}                                    | dashboard.video.destroy | App\Http\Controllers\InteractiveVideoController@destroy    |            |
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号