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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27168425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:28:45  来源:igfitidea点击:

Passing 2 Parameters to Laravel Routes - Resources

phplaravelroutesresources

提问by amof

I'm trying to build my routes using resources so that I can pass two parameters into my resources.

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

I'll give you a few examples of how the URLS would look:

我将举几个例子来说明 URLS 的外观:

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

So you can see I always need to have reference to the project_id and also the email/file id etc.

所以你可以看到我总是需要参考 project_id 以及电子邮件/文件 id 等。

I realize I can do this manually by writing all routes by hand, but I'm trying to stick to the resource model.

我意识到我可以通过手动编写所有路由来手动完成此操作,但我正在尝试坚持使用资源模型。

I figured something like this might work?

我想这样的事情可能有用吗?

Route::group(['prefix' => 'project'], function(){
  Route::group(['prefix' => '{project_id}'], function($project_id){

    // Files
    Route::resource('files', 'FileController');

  });
});

回答by Ronser

As far as I know about resources

据我所知资源

Route::resource('files', 'FileController');

The above mentioned resource will route the following urls.

上述资源将路由以下网址。

Few Actions Handled By Resource Controller for your Route::resource('files', 'FileController');

资源控制器为您处理的操作很少 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

the single resourceMentioned above will do all the listed routing

resource上面提到的单一将完成所有列出的routing

Apart from those you have to write your custom route

除了那些你必须写你的 custom route

In your scenario of

在你的场景中

Route::group(['prefix' => 'project'], function(){
  Route::group(['prefix' => '{project_id}'], function($project_id){

    // Files
    Route::resource('files', 'FileController');

  });
}); 

domain.com/project/100/files

if its a getrequest will be routed to FileController@index
if its a postrequest will be routed to FileController@store

domain.com/project/100/files

如果它的get请求将被路由到FileController@index
如果它的post请求将被路由到FileController@store

if your "domain.com/project/100/file/56968" is changed to "domain.com/project/100/files/56968"(file to files)then the following rooting will occur...

如果您的“ domain.com/project/100/file/56968”更改为“ domain.com/project/100/files/56968(文件到文件),则会发生以下生根...

domain.com/project/100/files/56968

if its a getrequest will be routed to FileController@show
if its a putrequest will be routed to FileController@update
if its a deleterequest will be routed to FileController@destroy

domain.com/project/100/files/56968

如果它的get请求将被路由到FileController@show
如果它的put请求将被路由到FileController@update
如果它的delete请求将被路由到FileController@destroy

and it has no impact on any other urls you have mentioned

并且它对url您提到的任何其他s没有影响

Provided, you need to have RESTful Resource Controllers

前提是你需要有RESTful Resource Controllers

回答by zhekaus

For the request like '/project/100/file/56968', you must specify your route like this:

对于像“/project/100/file/56968”这样的请求,你必须像这样指定你的路由:

Route::resource('project.file', 'FileController');

And then you can get parameters at the show method of the controller:

然后就可以在控制器的show方法中获取参数了:

public function show($project, $file) {
    dd([
        '$project' => $project,
        '$file' => $file
    ]);
}

The result of this example will be:

这个例子的结果将是:

array:2 [▼
  "$project" => "100"
  "$file" => "56968"
]