zend框架3中不同api版本的路由路径

时间:2017-11-08 12:46:44

标签: php zend-framework3 zend-route

这就是我为api定义路线的方法。它以/ api / v1为前缀。但是现在在api v2中添加了很少的新模块,并且所有v1 api都保持不变并且在v2中可用。我如何修改将为/ api / v1所有路由提供服务的路由以及何时调用/ api / v1,并且在调用/ api / v2时它应同时服务于/ api / v2和/ api / v1?

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

2 个答案:

答案 0 :(得分:2)

您可以将常用v1v2移到单个父路线和v2 - 只移到另一个父路线。以下是可帮助您理解该想法的示例(未经测试)代码。

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
    ]
];

答案 1 :(得分:1)

我知道这已经很晚了,但我刚发现了这个问题。

虽然@ gsc的答案有点可以,但这不是正确答案。

这是正确的答案,这就是我使用它的方式:

            'api' => [
            /** Our main route is /api **/
            'may_terminate' => true, 
            'options' => ['route' => '/api'],
            'type' => Literal::class, 
            'child_routes' => [
                /** Since our main route is /api, this will become /api/v1/YOUR_ACTIONS **/
                'v1' => [
                    'type'    => Segment::class,
                    'options' => [
                        'route'    => '/v1[/:action]',
                        'constraints' => [
                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ],
                        'defaults' => [
                            'controller'    => Controller\ApiV1Controller::class,
                            'action'        => 'index',
                        ],
                    ],
                ],
                 /** Since our main route is /api, this will become /api/v2/YOUR_ACTIONS **/
                'v2' => [
                    'type'    => Segment::class,
                    'options' => [
                        'route'    => '/v2[/:action]',
                        'constraints' => [
                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ],
                        'defaults' => [
                            'controller'    => Controller\ApiV2Controller::class,
                            'action'        => 'index',
                        ],
                    ],
                ],
                /** Add as many "versions" as you want, all with different controllers. **/
            ],
        ],

这允许您使用控制器的不同“版本”,并且更短,更易于理解并符合标准。

享受!