2

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. Problem is, the AbstractRestfulController keeps overriding these routes to the default actions get(), getList(), etc.

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?

Also, I would like to continue extending this controller because I am actually extending a more generic controller which extends this Zend one.

shiznatix
  • 1,087
  • 1
  • 20
  • 37
  • Why not use the [ZF2 `AbstractActionController`](https://github.com/zendframework/zend-mvc/blob/master/src/Controller/AbstractActionController.php) for this? – Wilt Oct 15 '15 at 08:17
  • @Wilt if you read my last sentence, I have a larger base controller that does the extending from Zends controller. I can do funkier routing inside the controllers but it would be cleaner and nicer to do it properly – shiznatix Oct 15 '15 at 09:56
  • Sorry, no I missed that important part. I will try to come up with a usable answer. – Wilt Oct 15 '15 at 09:57

1 Answers1

0

Try to set: 'may_terminate' => false

Right now your route will match 'my-endpoint' or 'single' and since there is 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.

Wilt
  • 41,477
  • 12
  • 152
  • 203