我的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
]
]
],
],
用户/登录错误。注意它正在寻找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"
}
}
如果我在urlManager中注释掉了ID令牌,则用户/登录操作有效,但其他路由失败。
通过将规则分为每个控制器一个项目来解决:
[
'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'
],
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句