2014-11-06 101 views
0

我在zend framework2中遇到了一些麻烦,为我的安静api设置路线。我想以这种格式创建路线:api-rest/wall/user_id/post [/:id]。ZF2安静的API儿童路线

现在,这是我的配置文件:

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

回答

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

在此配置中的用户ID是强制性的,不可选。

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

您可能还需要采取routing documentation中的外观儿童路线使用情况。