yii2 Restful API 路由美化配置步骤
1,在index.php脚本文件同级目录下添加.htaccess文件,添加规则使url隐藏入口脚本生效
RewriteEngine on # If a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Otherwise forward it to index.php RewriteRule . index.php`
2.为 config目录的web.php 增加配置项 urlManager,实例代码:
'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => true, 'showScriptName' => false, 'rules' => [ [ 'class' => 'yii\rest\UrlRule', 'controller' => 'article', 'ruleConfig'=>[ 'class'=>'yii\web\UrlRule', 'defaults'=>[ 'expand'=>'createdBy', ] ], 'extraPatterns'=>[ 'POST search' => 'search' ], ], ['class'=>'yii\rest\UrlRule', 'controller'=>'top10', 'except'=>['delete','create','update','view'], 'pluralize'=>false, ], ['class'=>'yii\rest\UrlRule', 'controller'=>'adminuser', 'except'=>['delete','create','update','view'], 'pluralize'=>false, 'extraPatterns' => [ 'POST login' => 'login', ] ], ], ],