不得不说yii真是反人类,怪不得现在都不维护了,今天解析下路由美化下的路由访问问题。
设置main.php配置文件
'urlManager' => ['enablePrettyUrl' => true, // 启用 URL美化'showScriptName' => false, // 隐藏入口文件index.php'enableStrictParsing' => true, // 是否开启严格解析,该选项仅在开启美化功能后生效'rules' => require(__DIR__ . '/url-rules.php'),],
设置路由文件url-rules.php
[['class' => 'yii\rest\UrlRule','controller' => ['scm/user'],'extraPatterns' => ['POST login' => 'login','POST register' => 'register','GET check-status' => 'check-status',]],['class' => 'yii\rest\UrlRule','controller' => ['scm/supplier-goods'],'extraPatterns' => ['GET list' => 'index','GET goods' => 'goods',]]
]
如上图,如果开启了路由美化,那么控制器的路由后缀必须是负数形式,也就是必须是s结束,如果直接访问scm/user会直接找不到
我们也可以不启用复数形式只需要在路由里添加
'class' => 'yii\rest\UrlRule','controller' => ['scm/user'],'pluralize' => false, //关闭复数形式'extraPatterns' => ['POST login' => 'login','POST register' => 'register',]
]
官方文档:
UrlRule, yii\rest\UrlRule | API Documentation for Yii 2.0 | Yii PHP Framework