ZF2 Route在扩展AbstractRestfulController的同时基于方法

时间:2015-10-14 20:48:22

标签: php zend-framework zend-framework2 zend-framework-routing

在Zend Framework 2中,我尝试根据请求类型将一些动态URL路由到扩展AbstractRestfulController的控制器中的指定操作。问题是,AbstractRestfulController会将这些路由覆盖为默认操作get()getList()等。

我的路线是:

GET /my-endpoint/{other_id} - allAction()
POST /my-endpoint/{other_id} - createAction()
GET /my-endpoint/{other_id}/{id} - getAction()
PUT /my-endpoint/{other_id}/{id} - updateAction()
DELETE /my-endpoint/{other_id}/{id} - deleteAction()

我的路由器配置是:

'my-endpoint' => [
    'type' => 'segment',
    'options' => [
        'route' => 'my-endpoint/:other_id',
        'constraints' => [
            'other_id' => '[0-9]+',
        ],
        'defaults' => [
            'controller' => 'my-endpoint',
        ],
    ],
    'may_terminate' => true,
    'child_routes' => [
        'get' => [
            'type' => 'method',
            'options' => [
                'verb' => 'get',
                'defaults' => [
                    'action' => 'all',
                ],
            ],
        ],
        'post' => [
            'type' => 'method',
            'options' => [
                'verb' => 'post',
                'defaults' => [
                    'action' => 'create',
                ],
            ],
        ],
        'single' => [
            'type' => 'segment',
            'options' => [
                'route' => '[/:id]',
                'constraints' => [
                    'id' => '[0-9]+',
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [
                'get' => [
                    'type' => 'method',
                    'options' => [
                        'verb' => 'get',
                        'defaults' => [
                            'action' => 'get',
                        ],
                    ],
                ],
                'update' => [
                    'type' => 'method',
                    'options' => [
                        'verb' => 'put',
                        'defaults' => [
                            'action' => 'update',
                        ],
                    ],
                ],
                'delete' => [
                    'type' => 'method',
                    'options' => [
                        'verb' => 'delete',
                        'defaults' => [
                            'action' => 'delete',
                        ],
                    ],
                ],
            ],
        ],
    ],
],

我的控制器有以下行动:

public function allAction() {
    die('allAction');
}

public function createAction() {
    die('createAction');
}

public function getAction() {
    die('getAction');
}

public function updateAction() {
    die('updateAction');
}

public function deleteAction() {
    die('deleteAction');
}

如何以这种方式专门路由,以便该控制器不允许其他任何请求类型/覆盖默认的AbstractRestfulController路由?

另外,我想继续扩展这个控制器,因为我实际上扩展了一个更通用的控制器,扩展了这个Zend。

1 个答案:

答案 0 :(得分:0)

尝试设置:'may_terminate' => false

现在,您的路线将匹配'my-endpoint''single',因为这些匹配有no action set。相反,它将get the http method from the request并映射到相应的控制器方法inside the AbstractRestfulController onDispatch method