Restful API Zend Framework 2

时间:2012-11-30 10:02:14

标签: php zend-framework2 restful-url zend-rest zend-rest-route

我已经想出如何通过AbstractRestfulController建立一个可以访问的简单资源。例如:

localhost/products - >名单
localhost/products/1 - >特殊产品

有没有办法筑巢资源?如果是这样,你会怎么做?例如:

localhost/products/1/photos - >列出产品的所有照片
localhost/products/1/photos/3124 - >显示产品的特殊照片

(我的目标是presentation}

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您需要添加其他路线。例如:

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

答案 1 :(得分:0)

'products' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/products/:productId[/photos/:photos]',
                'constraints' => array(
                    'productId' => '[0-9]*',
                    'photos' => '[0-9]*'
                ),
                'defaults' => array(
                    'controller' => 'your contrller',
                ),
            ),
        ),