2014-11-06 112 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中的外觀兒童路線使用情況。