dide
dide

Reputation: 73

Zend Framework 2 RestfulController with additional actions

I use the AbstractRestfulController for a controller in my ZF2-Application. This controller implements create(), update() etc.

Is it possible to have actions next to those REST-Functions?

E.g. I want to have: url.com/model/id to get the model (this works already), but i want to be able to call url.com/model/doSomething as well.

I tried using child_routes, but it did not work:

'car' => array(
                            'type'         => 'literal',
                            'options'      => array(
                                    'route'       => '/car',
                                    'defaults'    => array(
                                            'controller' => 'CarDealer\Controller\Car',
                                            'action' => 'index'
                                    ),
                            ),
                            'child_routes' => array(
                                    'rest'    => array(
                                            'type'    => 'segment',
                                            'options' => array(
                                                    'route'       => '[/:id]',
                                                    'constraints' => array(
                                                            'id' => '[0-9]+',
                                                    ),
                                                    'defaults'    => array(
                                                            'controller' => 'CarDealer\Controller\Car',
                                                    ),
                                            ),
                                    ),
                                    'actions' => array(
                                            'type'    => 'segment',
                                            'options' => array(
                                                    'route'       => '[/:action]',
                                                    'constraints' => array(
                                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                            'id'     => '[0-9]+',
                                                    ),
                                                    'defaults'    => array(
                                                            'controller' => 'CarDealer\Controller\Car',
                                                            'action'     => 'index',
                                                    ),
                                            ),
                                    ),
                            ),
                    ),

I am pretty sure the above doesnt make so much sense, but I could not find the right hints to get things working.

Thanks for your help!

Upvotes: 1

Views: 1382

Answers (2)

Rob Allen
Rob Allen

Reputation: 12778

The AbstractRestfulController won't dispatch to an arbitrary action method, so you should use a child route and another controller.

Upvotes: 4

rAVi
rAVi

Reputation: 164

use WildCard Routes to do something that is not excepted. in controller get the wildcard parameters to make that action

Upvotes: 0

Related Questions