我正在尝试使用资源构建我的路线,以便我可以将两个参数传递到我的资源中。
我将举几个例子来说明 URL 的外观:
domain.com/dashboard
domain.com/projects
domain.com/project/100
domain.com/project/100/emails
domain.com/project/100/email/3210
domain.com/project/100/files
domain.com/project/100/file/56968
所以你可以看到我总是需要引用 project_id 以及电子邮件/文件 ID 等。
我意识到我可以通过手动编写所有路由来手动完成此操作,但我试图坚持使用资源模型。
我觉得这样的事情可能行得通吗?
Route::group(['prefix' => 'project'], function(){
Route::group(['prefix' => '{project_id}'], function($project_id){
// Files
Route::resource('files', 'FileController');
});
});
最佳答案
据我所知
Route::resource('files', 'FileController');
上述资源将路由以下 url。
资源 Controller 为您的 Route::resource('files', 'FileController');
Route::get('files',FileController@index) // get req will be routed to the index() function in your controller
Route::get('files/{val}',FileController@show) // get req with val will be routed to the show() function in your controller
Route::post('files',FileController@store) // post req will be routed to the store() function in your controller
Route::put('files/{id}',FileController@update) // put req with id will be routed to the update() function in your controller
Route::delete('files',FileController@destroy) // delete req will be routed to the destroy() function in your controller
上面提到的单个 resource
将执行所有列出的 routing
除了那些你必须编写你的自定义路由
在你的场景中
Route::group(['prefix' => 'project'], function(){
Route::group(['prefix' => '{project_id}'], function($project_id){
// Files
Route::resource('files', 'FileController');
});
});
domain.com/project/100/files
如果它的get
请求将被路由到FileController@index
如果它的 post
请求将被路由到 FileController@store
如果您的“domain.com/project/100/file/56968
”更改为“domain.com/project/100/files/56968
" (file to files) 然后会发生下面的生根...
domain.com/project/100/files/56968
如果它的get
请求将被路由到FileController@ show
如果它的put
请求将被路由到FileController@update
如果它是delete
请求将被路由到 FileController@destroy
并且它不会影响您提到的任何其他 url
假设,你需要有RESTful Resource Controllers
关于php - 将 2 个参数传递给 Laravel 路由 - 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27168425/