Yii2 Rest API additional templates with multiple controllers

I have two controllers in my API. Each of them has additional templates. All my actions are working correctly, except for the user login, which is defined in the additional templates.

<?
'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
            ]
        ]

    ],
],

      

user / login errors. Note that it is looking for the v1 / user / view action

 {
    "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"
    }
}

      

If I comment out the ID token in urlManager, the user / login action works, but my other routes don't work.

+3


source to share


2 answers


Solved by dividing rules per element for each controller:



[
    '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'
    ],
] 

      

+3


source


When you specify markers

'{id}' => '<id:\\w+>',

      

you override the default yii \ rest \ UrlRule tokens

'{id}' => '<id:\\d[\\d,]*>',

      



and start viewing the route in the next

'GET,HEAD {id}' => 'view',
/user/<id:\\w+>

      

where id is the word so / user / login translated into an action like

0


source







All Articles