带有多个控制器的 Yii2 Rest API 额外模式

rob*_*obr 3 yii2 yii2-advanced-app

我的 API 中有 2 个控制器。每个都定义了额外的模式。除了在额外模式中定义的用户登录之外,我的所有操作都正常工作。

<?
'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        [
            'class' => 'yii\rest\UrlRule',
            'controller' => [ 'v1/item', 'v1/user'], 
            'tokens' => [
                '{id}' => '<id:\\w+>', //commenting out this token allows login to return
                '{type}'=>'<type:\\w+>'
            ],
            'extraPatterns' => [
                'POST {id}/image/{type}' => 'image', //from the item controller
                'GET login' => 'login' // from the USER controller
            ]
        ]

    ],
],
Run Code Online (Sandbox Code Playgroud)

用户/登录错误也是如此。请注意,它正在寻找 v1/user/view 操作

 {
    "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/user/view",
        "code": 0,
        "type": "yii\\base\\InvalidRouteException"
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我在 urlManager 中注释掉 ID 令牌,则用户/登录操作有效,但我的其他路由失败。

rob*_*obr 5

通过将规则分成每个控制器的一个项目来解决:

[
    'class' => 'yii\rest\UrlRule',
    'controller' => 'v1/config', //, 
    'tokens' => [
        '{id}' => '<id:\\w+>',
        '{type}'=>'<type:\\w+>'
    ],
    'extraPatterns' => [
        'POST {id}/image/{type}' => 'image',
    ]
],

[
    'class' => 'yii\rest\UrlRule', 
    'controller' => 'v1/user', 
    'extraPatterns' => [
        'GET login' => 'login'
    ],
] 
Run Code Online (Sandbox Code Playgroud)