路由模型绑定可以与 RESTful 控制器一起使用吗? [英] Can route model binding be used with RESTful controllers?

查看:33
本文介绍了路由模型绑定可以与 RESTful 控制器一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel项目中一直使用 RESTful控制器.通过包括:

I have been using RESTful controllers in my Laravel project. By including:

Route::controller('things', 'ThingController')

在routes.php中,我可以在 ThingController 中定义函数,例如:

in my routes.php, I can define functions in the ThingController like:

public function getDisplay($id) {
    $thing = Thing::find($id)
    ...
}

,以便获取URL"... things/display/1"会自动定向到控制器功能.这似乎很方便,到目前为止对我来说一直很好.

so that GETting the URL "...things/display/1" would automatically be directed to the controller function. This seems pretty handy and has been working great for me so far.

我注意到我的许多控制器功能都是从从URL通过id获取模型开始的,我认为能够使用

I noticed many of my controller functions start with getting a model by id from the url, and I thought it would be nice to be able to use route model binding to do this for me instead. So I updated my routes.php to

Route::model('thing', 'Thing');
Route::controller('things', 'ThingController')

并将 ThingController 函数更改为

public function getDisplay($thing) {
    ...
}

我认为这可以按照我想要的方式神奇地工作(就像到目前为止我在Laravel中尝试过的所有其他方式一样),但是不幸的是,当我尝试使用 $ thing .这是应该可以正常工作的东西,还是我做错了,还是只能将路由模型绑定与在route.php中明确命名的路由一起使用?

I assumed this would magically work the way I wanted it to (like everything else I've tried so far in Laravel has) but unfortunately I get "Trying to get property of non-object" when I attempt to use $thing in the function. Is this something that should be able to work and I have just done it wrong, or can route model binding only work with routes explicitly named in routes.php?

推荐答案

如果您不介意URI路径,方法名称,并且只能使用 show edit update 方法,则可以使用资源控制器生成URI可以定义模型绑定的字符串.

If you don't mind with URI path, method name and just work only show, edit and update method, you can use Resource Controller to generate URI string which can define model binding.

routes.php 中更改为

Route::model('things', 'Thing');
Route::resource('things', 'ThingController');

您可以使用 php artisan route 命令查看所有URI

You can use php artisan routes command to see all URIs

$ artisan routes | grep ThingController
GET|HEAD things                | things.index               | ThingController@index
GET|HEAD things/create         | things.create              | ThingController@create
POST things                    | things.store               | ThingController@store
GET|HEAD things/{things}       | things.show                | ThingController@show
GET|HEAD things/{things}/edit  | things.edit                | ThingController@edit
PUT things/{things}            | things.update              | ThingController@update
PATCH things/{things}          |                            | ThingController@update

此后,您可以威胁参数作为 Thing 对象,而无需显式命名路由.

After that you can threat parameter as Thing object without explicitly name route.

/**
 * Display the specified thing.
 *
 * @param  Thing  $thing
 * @return mixed
 */
public function show(Thing $thing)
{
    return $thing->toJson();
}

如果要访问 ThingController @ show ,请传递您的模型ID,Laravel将自动检索它.

If you want to access ThingController@show, pass your model ID and Laravel will retrieve it automatically.

http://example.com/things/1

{"id":1,"type":"Yo!"}

这篇关于路由模型绑定可以与 RESTful 控制器一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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