yii2 Restful API 路由美化配置步骤

作者: wxfeng 分类: php 发布时间: 2018-11-16 00:00

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

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注