首页 > 技术文章 > Yii 2.0 GII 访问404错误

liangzia 2019-05-17 17:49 原文

网上大部分都是普通的开启和配置资料 
按照网上资料配置 访问localhost/index/php?r=gii 总是提示404错误 

解决方法如下: 
Yii基础版中的 web.php 代码如下

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        'allowedIPs' => ['127.0.0.1', '::1'],
    ];
}

注意这里的变量是YII_ENV_DEV 而在入口文件index.php中配置的是

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV','dev');
//需要根绝web.php中的变量YII_ENV_DEV增加对应的开启设置
defined('YII_ENV_DEV ') or define('YII_ENV_DEV ', true);

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/../config/web.php');

(new yii\web\Application($config))->run();

 

2.路由配置 
enablePrettyUrl 设置为false

//路由配置
        'urlManager' => [
           'enablePrettyUrl' => false,//路由的路径化 去掉 ?r=
           'showScriptName' => false,//隐藏入口Index.php文件
            //'suffix' => '.html',//假后缀(fake suffix) .html
            'rules' => [
                '<controller:\w+>/<name:\w+>/<action:\w+>'=>'<controller>/<action>',
                'company/<id:\d+>.html'=>'company/list',
            ],
        ],

 

3.GII是基于本地访问使用的 所有使用localhost或127.0.0.1来访问。

推荐阅读