无效的路线:无法解决请求Yi​​i2 Rest

时间:2018-11-26 12:26:42

标签: rest api yii2

我正在配置Yii2 Rest API应用程序。

这是我的API目录结构:

api
├── index.php
├── components
├── config
│   ├── api.php
│   ├── bootstrap.php
│   ├── params.php
│   └── routes.php
├── modules
│   └── v1
│       ├── Module.php
│       └── modules
│           ├── auth
│           │   └── controllers
│           │       └── MainController.php
│           └── Module.php
├── runtime
└── traits

api.php

...
'urlManager' => [
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        [
            'class' => 'yii\rest\UrlRule',
            'controller' => [
                'v1/auth/main'
            ],
            'extraPatterns' => [
                'POST v1/login' => 'login' // For MainController in Auth module
            ]
        ],
    ]
],
...
'modules' => [
    'v1' => [
        'class' => 'app\api\modules\v1\Module',
        // Sub modules for v1 module
        'modules' => [
            'auth' => [
                'class' => 'app\api\modules\v1\modules\auth\Module',
            ],
        ],
    ],
],
...

当我尝试向POST发出http://my.api/v1/login请求时,我在邮递员中得到的是:

{
    "name": "Not Found",
    "message": "Page not found.",
    "code": 0,
    "status": 404,
    "type": "yii\\web\\NotFoundHttpException",
    "previous": {
        "name": "Invalid Route",
        "message": "Unable to resolve the request \"v1/login\".",
        "code": 0,
        "type": "yii\\base\\InvalidRouteException"
    }
}

我知道我可以像下面这样处理这种情况,但这不是我想要的:

'urlManager' => [
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        'POST v1/login' => 'v1/auth/main/login'
    ]
],

0 个答案:

没有答案