dongtang6775 2018-08-30 07:57
浏览 36

too long

My Folder and code strucure is -

api/
    modules/
        v1/
            controllers/
                UserController.php
                BaseController.php
            Module.php
        v2/
            controllers/
                UserController.php
                BaseController.php
            Module.php

And my application configuration would look like:

'modules' => [
        'v1' => [
            'basePath' => '@app/modules/v1',
            'class' => 'api\modules\v1\Module'
        ],
         'v2' => [
            'basePath' => '@app/modules/v2',
            'class' => 'api\modules\v2\Module'
        ],
     ],
'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => false,
            'showScriptName' => false,
            'rules' => [

                    ['class' => 'yiiest\UrlRule', 'controller' => ['v2/user']],
                    ['class' => 'yiiest\UrlRule', 'controller' => ['v1/user']],
],
]

I am following the same procedue as given on yii2 doc But its versioning not working .

UPDATE : I have created a costume rule and parse according that . Looking for something else .

    class ApiUrlRule implements UrlRuleInterface {

    public function parseRequest($manager, $request) {
        $pathInfo = $request->getPathInfo();
        $paramas=$request->getQueryParams();


        $version=Yii::$app->response->acceptParams['version'];
        $route = Yii::$app->response->acceptParams['version'].'/'.$pathInfo;
         return [$route,$paramas];
    }

public function createUrl($manager, $route, $params) {

}


}
  • 写回答

1条回答 默认 最新

  • douyiavxxh02727 2018-08-30 11:14
    关注

    From the docs you linked:

    Both methods have their pros and cons, and there are a lot of debates about each approach. Below you'll see a practical strategy for API versioning that is a mix of these two methods:

    • Put each major version of API implementation in a separate module whose ID is the major version number (e.g. v1, v2). Naturally, the API URLs will contain major version numbers.

    • Within each major version (and thus within the corresponding module), use the Accept HTTP request header to determine the minor version number and write conditional code to respond to the minor versions accordingly.

    The major api version number is determined by the url, as you've set in your urlManager. You can access them by calling your-yii2-app.com/v1/controller/action or your-yii2-app.com/v2/controller/action. Any code that parses the header must be written by you:

    Accept HTTP request header to determine the minor version number and write conditional code to respond to the minor versions accordingly.

    If you want to write a custom api version header functionallity, this is possible. I suggest you try and create a new question if you run into trouble.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题