ZF2 Child路由for restful API

时间:2014-11-06 17:04:32

标签: api rest routes zend-framework2

我在zend framework2中为我的restful api设置路由时遇到了一些麻烦。我想以这种格式创建路线:api-rest / wall / user_id / post [/:id]。

现在这是我的配置文件:

'router' => array(
    'routes' => array(
        'api-rest' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/api-rest/wall[/:id]',
                'constraints' => array(
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'ApiRest\Controller\Wall',
                ),
            ),
        ),
    ),
),

1 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

'router' => array(
    'routes' => array(
        'api-rest' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/api-rest/wall/:userid/posts[/:id]',
                'constraints' => array(
                    'id'     => '[0-9]+',
                    'userid' => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'ApiRest\Controller\Wall',
                ),
            ),
        ),
    ),
),

在此配置中,userid是必需的,而不是可选的。

  • /api-rest/wall/3/posts/2 - ID为2的帖子和ID为3的用户
  • /api-rest/wall/3/posts - 用户ID为
  • 的所有帖子
  • /api-rest/wall/3 - 赢了

您可能还想查看routing documentation中的子路由使用情况。