简体   繁体   English

扩展AbstractRestfulController时基于ZF2路由的方法

[英]ZF2 Route based on method while extending AbstractRestfulController

In Zend Framework 2 I am trying to route a few dynamic urls to specified actions in a controller that extends AbstractRestfulController , based on the request type. 在Zend Framework 2中,我尝试根据请求类型将一些动态url路由到扩展AbstractRestfulController的控制器中的指定操作。 Problem is, the AbstractRestfulController keeps overriding these routes to the default actions get() , getList() , etc. 问题是, AbstractRestfulController会将这些路由覆盖为默认操作get()getList()等。

My routes are: 我的路线是:

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 router config is: 我的路由器配置是:

'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',
                        ],
                    ],
                ],
            ],
        ],
    ],
],

And my controller has the following actions: 我的控制器执行以下操作:

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');
}

How can I route specifically this way, so that no other request types are allowed to this controller / overriding the default AbstractRestfulController routes? 我该如何特别地通过这种方式进行路由,以使其他任何请求类型均不允许进入该控制器/覆盖默认的AbstractRestfulController路由?

Also, I would like to continue extending this controller because I am actually extending a more generic controller which extends this Zend one. 另外,我想继续扩展此控制器,因为实际上我正在扩展一个更通用的控制器,该控制器扩展了这个Zend。

Try to set: 'may_terminate' => false 尝试设置: 'may_terminate' => false

Right now your route will match 'my-endpoint' or 'single' and since there is no action set for these matches. 现在,您的路线将匹配'my-endpoint''single'并且因为没有为这些匹配设置动作 Instead it will get the http method from the request and map to the corresponding controller methods inside the AbstractRestfulController onDispatch method . 相反,它将从请求中获取http方法,并映射到AbstractRestfulController onDispatch方法内的相应控制器方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM