2017-11-08 10 views
0

내 API의 경로를 정의한 방법입니다. 접두어는/api/v1입니다. 하지만 이제는 api v2에 새로운 모듈이 거의 추가되지 않고 모든 v1 API는 동일하게 유지되고 v2에서 사용할 수 있습니다./api/v1이 호출 될 때/api/v2가 호출 될 때/api/v1 및/api/v1과/api/v1을 모두 제공해야하는이 경로를 수정하려면 어떻게해야합니까?젠드 프레임 워크 3에서 다른 API 버전의 경로 경로

당신은 다른 사람들 일반적인 v1 단일 부모 노선 v2v2 - 단지 사람을 이동할 수 있습니다

'product' => array(
    'type' => 'Zend\Router\Http\Segment', 
    'options' => array(
     'route' => '/api/v1/categories[/:id]', 
     'defaults' => array(
      'controller' => CategoryController::class, 
     ), 
    ), 
), 
'products' => array(
    'type' => 'Zend\Router\Http\Segment', 
    'options' => array(
     'route' => '/api/v1/products[/:id]', 
     'defaults' => array(
      'controller' => ProductsController::class, 
     ), 
    ), 
), 

// ... at lots of v1 apis 

//these are introduced in v2 
'trends' => array(
    'type' => 'Zend\Router\Http\Segment', 
    'options' => array(
     'route' => '/api/v2/trends[/:id]', 
     'defaults' => array(
      'controller' => TrendsController::class, 
     ), 
    ), 
), 

답변

1

module.config.php. 아래는 아이디어를 이해하는 데 도움이되는 샘플 코드입니다. 당신이 아이의 경로를 사용하지 않으려면

return [ 
    // in Config.router.routes 
    'api' => [ 
     'child_routes' => [ 
      'v1' => [ 
       'child_routes' => [ 
        // your API 1-and-2 routes 
        'product' => [/* … */], 
        'products' => [/* … */] 
       ], 
       'may_terminate' => false, 
       'options' => [ 
        'constraints' => ['version' => 'v1|v2'], 
        'route'  => '/:version' 
       ], 
       'type' => Segment::class 
      ], 
      'v2' => [ 
       'child_routes' => [ 
        // your API 2 routes 
        'trends' => [/* … */] 
       ], 
       'may_terminate' => false, 
       'options' => ['route' => '/v2'], 
       'type' => Literal::class 
      ] 
     ], 
     'may_terminate' => false, 
     'options' => ['route' => '/api'], 
     'type' => Literal::class 
    ] 
]; 

, 당신은 단순히 /v1 대신 경로 매개 변수/제약 조건을 추가 할 수 있습니다

return [ 
    'product' => [ 
     'options' => [ 
      'constraints' => [ 
       'id'  => '…', 
       'version' => 'v1|v2' 
      ], 
      'defaults' => ['controller' => CategoryController::class], 
      'route' => '/api/:version/categories[/:id]' 
     ], 
     'type' => Segment::class 
    ] 
];