【问题标题】:laravel 4: difference between resource and controller in Route classlaravel 4:Route类中资源和控制器之间的区别
【发布时间】:2014-08-02 11:11:09
【问题描述】:

静态路由方法“resource”和“controller”有什么区别

Route::controller()

Route::resource()

谢谢,

【问题讨论】:

标签: laravel laravel-4 laravel-routing


【解决方案1】:

我得到了一些东西:

Route::resource()
  • 强制您使用默认方法(索引、创建、存储、显示、编辑、更新、销毁),而无法在控制器类中添加新方法(无法调用新方法)

但是

Route::controller()
  • 让您在控制器类中定义无限的方法
  • 需要在函数名称前定义使用的 HTTP 动词,例如 (postCreate, anyCreate)

【讨论】:

    【解决方案2】:

    以下是两者都执行时发生的路由:

    Route::controller('test', 'TestController');
    Route::resource('othertest', 'OtherTestController');
    

    如果更容易的话,这是我将要以文字形式为您写出的图片:

    以下是多合一的。例如,如果您将GET 转换为laravel_dir/test/page,它将在TestController 中查找方法getPage()。如果你 POSTlaravel_dir/test/page,它将寻找 postPage()

    URI:GET|HEAD|POST|PUT|PATCH|DELETE 测试/{_missing}

    路线名称:无

    动作:TestController@missingMethod

    以下是资源路由的结果...您会发现它对您的 routes.php 文件的一行中的 CRUD 非常有用。

    URI:GET|HEAD othertest

    路线名称:othertest.index

    动作:OtherTestController@index


    URI:GET|HEAD othertest/create

    路线名称:othertest.create

    动作:OtherTestController@create


    URI:POST othertest

    路线名称:othertest.store

    动作:OtherTestController@store


    URI:GET|HEAD othertest/{othertest}

    路线名称:othertest.show

    动作:OtherTestController@show


    URI:GET|HEAD othertest/{othertest}/edit

    路线名称:othertest.edit

    动作:OtherTestController@edit


    URI:PUT othertest/{othertest}

    路线名称:othertest.update

    动作:OtherTestController@update


    URI:PATCH othertest/{othertest}

    Route Name:othertest.update(与上面同名)

    动作:OtherTestController@update


    URI:删除其他测试/{othertest}

    路线名称:othertest.destroy

    动作:OtherTestController@destroy

    【讨论】:

      【解决方案3】:

      您可以在官方文档中阅读相关内容:

      http://laravel.com/docs/controllers#restful-controllers

       Route::controller()
      

      它会将您定义的所有路由声明为以 html 动词开头的函数,例如文档中的示例:

      Route::controller('users', 'UserController');
      
        class UserController extends BaseController {
      
        public function getIndex()
        {
          //
        }
      
        public function postProfile()
        {
          //
        }
      
        public function anyLogin()
        {
          //
        }
      
      }
      

      另一方面:

      http://laravel.com/docs/controllers#resource-controllers

      Route::resource()
      

      基本上是在使用artisan的create controller命令时使用:

      php artisan controller:make PhotoController
      

      它将生成所有由 artisan 命令生成的路由,基本上是 crud 路由。

      希望对你有帮助。

      【讨论】:

        【解决方案4】:

        此方法自动检测“GET”、“POST”、“PUT/PATCH”、“DELETE”方法。

        Route::resource()
        

        此方法自动检测来自 URL 的参数

        Route::controller()
        

        也看一下:Laravel 4 : Route to localhost/controller/action

        【讨论】:

        • 我认为两者的交互方式相同(使用 HTTP 动词)
        猜你喜欢
        • 1970-01-01
        • 2019-07-10
        • 1970-01-01
        • 2016-09-21
        • 2013-07-30
        • 1970-01-01
        • 2016-05-21
        • 2013-01-13
        • 1970-01-01
        相关资源
        最近更新 更多