Restful API Zend Framework 2

671 Views Asked by At

I've already figured out how to make a simple resource reachable by AbstractRestfulController. Example:

localhost/products -> list
localhost/products/1 -> special product

Is there a way to nest resources? If so, how would you do that? Example:

localhost/products/1/photos -> list all photos of a product
localhost/products/1/photos/3124 -> show special photo of a product

(I have in this presentation as goal mind)

Thanks for your help!

2

There are 2 best solutions below

0
Michael Gallego On

You need to add another route. For instance :

'products' => array(
                        'type'    => 'Literal',
                        'options' => array(
                            'route'    => '/products',
                            'defaults' => array(
                                'controller' => 'Application\Controller\ProductsRest',
                                'action'     => null
                            )
                        ),
                        'may_terminate' => true,
                        'child_routes'  => array(
                            'photos' => array(
                                'type'    => 'Segment',
                                'options' => array(
                                    'route' => '/:productId/photos'
                                )
                            ),                                
                        )
                    )
0
Dharmesh Vasani On
'products' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/products/:productId[/photos/:photos]',
                'constraints' => array(
                    'productId' => '[0-9]*',
                    'photos' => '[0-9]*'
                ),
                'defaults' => array(
                    'controller' => 'your contrller',
                ),
            ),
        ),