php - 将 2 个参数传递给 Laravel 路由 - 资源

标签 php laravel routes resources

我正在尝试使用资源构建我的路线,以便我可以将两个参数传递到我的资源中。

我将举几个例子来说明 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/

相关文章:

php - 搜索特定关键字 MySQL

laravel - 从 storage/app/public/photos 中删除 Laravel 文件

php - Laravel:Eloquent Search 无法正常工作

c# - MVC 3 如何使用 MapRoute

ruby-on-rails-3.2 - ruby 路轨如何获取到命名路线的路径

php - JavaScript 函数没有响应

php - 在 gentoo linux 中启用 php curl

php - 如何选择所有表格的行?

testing - 带有指令的angular 2测试组件并得到错误路由器加载

php - 无法找到PHP为MySQL错误号定义