将2个参数传递给Laravel路线-资源 [英] Passing 2 Parameters to Laravel Routes - Resources

查看:66
本文介绍了将2个参数传递给Laravel路线-资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我将为您提供一些有关URL外观的示例:

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

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

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

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

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

  });
});

推荐答案

据我了解的资源

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

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

The above mentioned resource will route the following urls.

资源控制器为您的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

the single resource Mentioned above will do all the listed routing

除了那些你必须写的custom route

在您的情况下

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

if its a get request will be routed to FileController@index
if its a post request will be routed to FileController@store

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

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/files/56968

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

domain.com/project/100/files/56968

if its a get request will be routed to FileController@show
if its a put request will be routed to FileController@update
if its a delete request will be routed to FileController@destroy

,它对您提到的任何其他url都没有影响

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

已提供,您需要具有 RESTful资源控制器

这篇关于将2个参数传递给Laravel路线-资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆