laravel资源控制器/路线和模型 [英] laravel resource controllers / routes and models

查看:93
本文介绍了laravel资源控制器/路线和模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,如果您做类似的事情

So if you do something like

$ artisan make:model TurboClown
$ artisan make:controller TurboClownController -r --model=TurboClown

因此,此时可以添加类似的内容:

So at this point it's possible to add something like:

Route::resource('clowns','TurboClownController');

致您的routes/web.php.现在,当我与工匠一起routes:list时,我有了类似clowns/{clown}的路线.

To your routes/web.php. Now I have routes like clowns/{clown} when I routes:list with artisan.

但是,我的show函数类似于:

However, my show function is like:

    public function show(TurboClown $turboClown)

例如,当您请求return $turboClown哪个会给出[]作为响应.我花了一段时间才弄清楚,如果将$turboClown参数更改为$clown,我将得到一个JSON TurboClown作为响应.

Which when you return $turboClown will just give [] as a response when you request "/clowns/3/" for example. It took me a while to figure out that if I change $turboClown parameter to $clown, I get a JSON TurboClown as a response.

从某种意义上说,我解决了我的问题.但我对以下几点感到好奇:

So in a sense, I solved my problem. But I'm curious about a couple points:

  1. 对我来说, https://laravel .com/docs/5.4/controllers#restful-naming-resource-route-parameters 的读取方式似乎是我可以添加['parameters' => ['clown' => 'turboClown'],然后show()可以使用"turboClown"工作,但不能.

  1. To me, https://laravel.com/docs/5.4/controllers#restful-naming-resource-route-parameters reads as if I could add ['parameters' => ['clown' => 'turboClown'] and then show() would work using "turboClown", but it does not.

根据 https://laravel.com/docs/5.4/routing#route-parameters :路由参数根据它们的顺序注入到路由回调/控制器中-回调/控制器参数的名称无关紧要."但是在这种情况下,参数的名称似乎很重要吗?

According to https://laravel.com/docs/5.4/routing#route-parameters : " Route parameters are injected into route callbacks / controllers based on their order - the names of the callback / controller arguments do not matter." But it looks like the name of the parameter does matter in this case?

所以我想对这两点进行某种澄清,我想念的是什么?

So I am looking for some kind of clarification on those two points, what am I missing?

推荐答案

  1. 您做错了.该文档说:

参数数组应该是资源名称和参数名称的关联数组

The parameters array should be an associative array of resource names and parameter names

因此,在您的情况下,资源名称为小丑"(而不是小丑"),并且您希望该资源名称的参数为"turboClown:

So in your case the resource name is "clowns" (and not "clown") and you want for this resource name the parameter to be "turboClown:

Route::resource('clowns', 'TurboClownController', ['parameters' => [
    'clowns' => 'turboClown'
]]);

  1. 我同意这一点可能会造成混淆.也许您已经知道,但是您在这里所做的叫做隐式路由模型绑定.为了使它隐含",有一条规则可以否决您提到的一条规则:
  1. I agree that this point can be confusing. Maybe you already know that but what you are doing here is called implicit route model binding. And for it to be "implicit", there is one rule overruling the one you mentioned:

Laravel自动解析在路由或控制器动作中定义的口才模型,这些动作或类型的变量名与路径段名称匹配.

Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.

同样,要使此功能很酷,参数必须匹配相应模型的标识符(在您的情况下为TurboClown).

Also for this pretty cool thing to work the parameter has to match an identifier for the corresponding model (in your case TurboClown).

因此,现在结合这两点,您应该可以做到:

So now combining those two points you should be able to do this:

在您的路线文件中:

Route::resource('clowns', 'TurboClownController', ['parameters' => [
    'clowns' => 'turboClown'
]]);

TurboClownController中:

public function show(TurboClown $turboClown)
{
    return $turboClown;
}

现在,假设您要检索标识为5的涡轮小丑.您可以计算路线http://example.dev/clowns/5.

Now let's say you want to retrieve the turbo clown with the identifier 5. You can cal the route http://example.dev/clowns/5.

希望有帮助.

这篇关于laravel资源控制器/路线和模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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