gpt4 book ai didi

php - Laravel 5 Route::resource 嵌套 Route::group

转载 作者:行者123 更新时间:2023-12-02 20:20:08 24 4
gpt4 key购买 nike

此结构适用于(所有 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');
});
...
});

最佳答案

执行时问题会变得很明显

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

正如您所见,在视频组下将视频路由定义为 / 是行不通的,因为为了资源 Controller 创建带参数的静态路径,它使用路径的第一部分作为变量名。在您的情况下,它是空的(只是一个被删除的斜杠 /),这就是您得到这个 {} 的原因。

您必须更改您的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 元素下,否则 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 | |

关于php - Laravel 5 Route::resource 嵌套 Route::group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30520605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com