ZF2 Route 基于方法同时扩展 AbstractRestfulController

ZF2 Route based on method while extending AbstractRestfulController

在 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 控制器。

尝试设置:'may_terminate' => false

现在您的路线将匹配 'my-endpoint''single',因为有 no action set for these matches. Instead it will get the http method from the request and map to the corresponding controller methods inside the AbstractRestfulController onDispatch method.