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

时间:2015-05-29 03:26:41

标签: php laravel-5

此结构适用于(所有GET):/dashboard/video/dashboard/video/element/dashboard/video/create/dashboard/video/element/create/dashboard/video/element/{id},但不适用于/dashboard/video/{id}

换句话说,show($id)没有调用/dashboard/video/{id}

知道为什么吗?

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 个答案:

答案 0 :(得分:5)

执行

时,问题会变得很容易看到
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/{}                                    | 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    |            |

正如您所看到的那样,将视频路由定义为视频组下的/将无法正常工作,只是因为为了让资源控制器创建带参数的restful路径,它将路径的第一部分用作变量名称。在你的情况下,它是空的(只是一个被剥离的斜杠/),这就是你得到这个{}的原因。

您必须更改您的routes.php并将视频资源移出群组,它会起作用,如下所示:

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');

});

您必须将视频资源放在视频元素下,否则video/element永远不会匹配,因为定义的路由顺序很重要。

这对您有用,现在所有页面都会正常打开。您还可以通过再次执行来检查路由是否正确定义:

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    |            |