laravel资源控制器/路由和模型

时间:2017-08-08 21:15:38

标签: php laravel

所以,如果你做了像

这样的事情
<table  class="inline_sd_table"> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Accessories Optional</b></td>      <td class="inline_sd_even_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Accommodates Ceiling Height</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>ADA Compliance</b></td>      <td class="inline_sd_even_cell">See Additional Information above</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Blower Air Mover Type</b></td>      <td class="inline_sd_odd_cell">Fan</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Blower Included</b></td>      <td class="inline_sd_even_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Boost Mode</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>CFM (Airflow)</b></td>      <td class="inline_sd_even_cell">190</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Color/Finish</b></td>      <td class="inline_sd_odd_cell">Almond</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Control Feature Filter Reminder</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Damper Included</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Delay Shut Off</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Digital Clock</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Dishwasher Safe Filters</b></td>      <td class="inline_sd_even_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Duct Connector Size</b></td>      <td class="inline_sd_odd_cell">3-1/4" x 10", 7" Round Non-Ducted</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>ENERGY STAR&reg; qualified</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Filter Included</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Filter Type</b></td>      <td class="inline_sd_even_cell">Aluminum</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Flue Accommodates ceilings up to xx ft</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Heat Sentry - Temperature Sensor</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>HVI Certified</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Light Bulb Quantity</b></td>      <td class="inline_sd_even_cell">1</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Light Bulbs Included</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Lighting Function</b></td>      <td class="inline_sd_even_cell">One-level</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Lighting Type</b></td>      <td class="inline_sd_odd_cell">Incandescent</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Max Sones/100 CFM</b></td>      <td class="inline_sd_even_cell">4.1</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Minimum Height Above Cook Top</b></td>      <td class="inline_sd_odd_cell">18"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Mounting Type</b></td>      <td class="inline_sd_even_cell">Under Cabinet</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Nightlight</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Non-Stick Coated</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Product Depth (inches)</b></td>      <td class="inline_sd_odd_cell">17-1/2"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Product Height (inches)</b></td>      <td class="inline_sd_even_cell">6"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Product Width (inches)</b></td>      <td class="inline_sd_odd_cell">42"</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Rated Amps</b></td>      <td class="inline_sd_even_cell">2.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Remote Control</b></td>      <td class="inline_sd_odd_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Sones Certified Horizontal Rectangular High</b></td>      <td class="inline_sd_even_cell">6.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Sones Certified Horizontal Rectangular Low</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Sones Certified Vertical Rectangular High</b></td>      <td class="inline_sd_even_cell">6.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Sones Certified Vertical Rectangular Low</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Sones Certified Vertical Round High</b></td>      <td class="inline_sd_even_cell">6.5</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Sones Certified Vertical Round Low</b></td>      <td class="inline_sd_odd_cell">N/A</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Switch Control Settings</b></td>      <td class="inline_sd_even_cell">2</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>Switch Style</b></td>      <td class="inline_sd_odd_cell">Rocker</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Timer</b></td>      <td class="inline_sd_even_cell">No</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_odd_cell"><b>UL Listed</b></td>      <td class="inline_sd_odd_cell">Yes</td> </tr> <tr class="inline_sd_cell_row">      <td class="inline_sd_even_cell"><b>Voltage</b></td>      <td class="inline_sd_even_cell">120</td> </tr> </table> 

所以在这一点上可以添加如下内容:

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

Route::resource('clowns','TurboClownController'); 。现在,当routes/web.php与工匠合作时,我有clowns/{clown}这样的路线。

但是,我的routes:list功能就像:

show

当您 public function show(TurboClown $turboClown) 提出return $turboClown作为回复时,当您请求&#34; / clowns / 3 /&#34;例如。我花了一段时间才弄清楚如果我将[]参数更改为$turboClown,我会得到一个JSON TurboClown作为回复。

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

  1. 对我而言,https://laravel.com/docs/5.4/controllers#restful-naming-resource-route-parameters读起来好像我可以添加$clown,然后['parameters' => ['clown' => 'turboClown']可以使用&#34; turboClown&#34;,但它没有。< / p>

  2. 根据https://laravel.com/docs/5.4/routing#route-parameters:&#34;路由参数根据其顺序注入到路由回调/控制器中 - 回调/控制器参数的名称无关紧要。&#34;但看起来参数的名称在这种情况下确实很重要吗?

  3. 所以我正在寻找关于这两点的某种澄清,我错过了什么?

1 个答案:

答案 0 :(得分:4)

  1. 你做错了。文件说:
  2.   

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

    因此,在您的情况下,资源名称是“clowns”(而不是“clown”),并且您希望此资源名称参数为“turboClown:

    Route::resource('clowns', 'TurboClownController', ['parameters' => [
        'clowns' => 'turboClown'
    ]]);
    
    1. 我同意这一点可能令人困惑。也许你已经知道了,但你在这里做的是implicit route model binding。而且由于它是“隐含的”,有一条规则推翻你提到的那条规则:
    2.   

      Laravel自动解析路径或控制器操作中定义的Eloquent模型,其类型提示的变量名称与路径段名称匹配。

      对于这个非常酷的工作,参数必须匹配相应模型的标识符(在您的情况下为TurboClown)。

      所以现在结合这两点你应该能够做到这一点:

      在路线档案中:

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

      TurboClownController

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

      现在让我们假设您要检索标识符为5的turbo小丑。您可以对路线http://example.dev/clowns/5进行校验。

      希望它有所帮助。