我的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令牌,则用户/登录操作有效,但我的其他路由失败。
答案 0 :(得分:4)
通过将规则分成每个控制器的项目来解决:
[
'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'
],
]
答案 1 :(得分:0)
指定令牌时
'{id}' => '<id:\\w+>',
您覆盖默认的yii \ rest \ UrlRule标记
'{id}' => '<id:\\d[\\d,]*>',
并查看路线开始转换为以下
'GET,HEAD {id}' => 'view',
/user/<id:\\w+>
其中id是一个单词,因此/ user / login被转换为view action