dongyue9864 2017-08-08 21:15
浏览 140
已采纳

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

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

However, my show function is like:

    public function show(TurboClown $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. 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.

  2. 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条回答 默认 最新

  • dtoq41429 2017-08-08 21:57
    关注
    1. You are doing it wrong. The documentation says:

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

    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. 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 automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.

    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:

    In your routes file:

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

    In TurboClownController:

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

    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.

    Hope it helped.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上