php - AbstractRestfulController 和 AbstractActionController 有什么区别?

标签 php zend-framework2

从实际的角度来看,使用 AbstractRestfulController 与 AbstractActionController 来实现 Controller 类有什么区别? AbstractRestfulController 是否只允许 RESTful 行为?是否有并排比较的地方?

最佳答案

ZF2 的 AbstractRestfulController 提供了一个接口(interface),可以快速轻松地实现基本 HTTP 方法(例如 GET、POST、PUT、DELETE),同时仍然允许使用 Controller 操作。 AbstractActionController 旨在与 Controller 操作一起使用。

首先,无论该特定 Controller 中实现了多少 native HTTP 方法,都只需要一个路由。下面是一个路由配置示例:

'api_customer' => [
    'type' => 'Segment',
    'options' => [
        'route' => '/customer/:id',
        'constraints' => [
            'id' => '[0-9]+',
        ],
        'defaults' => [
            '__NAMESPACE__' => 'Customer\Controller',
            'controller' => 'Index',
        ],
    ],
],

最后, Controller 的实现相当简单(并且应该有点熟悉)。

<?php
class CustomerController extends AbstractRestfulController
{
    public function get($id)
    {
        // associated with GET request with identifier
    }

    public function getList()
    {
        // associated with GET request without identifier
    }

    public function create($data)
    {
        // associated with POST request
    }

    public function update($id, $data)
    {
        // associated with PUT request
    }

    public function delete($id)
    {
        // associated with DELETE request
    }
}

有关更多信息,我建议快速浏览一下手册。 http://framework.zend.com/manual/current/en/modules/zend.mvc.controllers.html#the-abstractrestfulcontroller

关于php - AbstractRestfulController 和 AbstractActionController 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34532468/

相关文章:

php - 将我的 Controller 移动到 laravel 5 中的子文件夹后,获取 Controller 不存在

php - 在 PHP 应用程序 Openshift 上提交更改后出现 404 Not Found 错误

php - View Helper 中的 Zend Framework 2 服务

php - 如何将 PHP 命名空间与自动加载一起使用?

PHP - 如何获取 n 深多维数组的所有数组值?

php - 如何使用 php curl 将远程图像保存到文件?

php - 使用工厂时,zf2 新模块 Controller 解析为无效的 Controller 类或别名

postgresql - Doctrine2 使我对 OneToMany 和 ManyToOne 关系感到疯狂

php - Zend Framework 2 控制台路由

zend-framework2 - 如何在 zend framework 2 中跨子域共享 zfcuser