2017-11-08 2 views
0

これは私のAPIのルートを定義した方法です。接頭辞は/ api/v1です。しかし、いくつかの新しいモジュールがapi v2に追加され、すべてのv1 apisはそのままでv2でも利用可能です。どのルートを/ api/v1に属し、/ api/v1が呼び出されると、/ api/v2が呼び出されるときに/ api/v2と/ api/v1の両方に役立つはずです。あなたが別の単一親ルートとv2のみのものにそれらの共通v1v2を移動することができますzendフレームワーク3の異なるAPIバージョンのルートパス

module.config.php

'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

。以下は、そのアイデアを理解するのに役立つサンプルコードです(テストされていません)。

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 
    ] 
]; 
関連する問題