我在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',
),
),
),
),
),
答案 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中的子路由使用情况。