用于restful API的ZF2子路由



我在zendframework2中为我的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' => 'ApiRestControllerWall',
                ),
            ),
        ),
    ),
),

您可以尝试以下操作:

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

在此配置中,userid是强制性的,而不是可选的。

  • /api-rest/wall/3/posts/2-id为2的帖子和id为3的用户
  • /api-rest/wall/3/posts-id为3的用户的所有帖子
  • /api-rest/wall/3-不匹配

您可能还想在路由文档中查看子路由的使用情况。

最新更新