Yii2 Gii禁止代码403您无权访问此页面

时间:2014-10-11 14:38:44

标签: php yii2 gii

我有一台server计算机,我正在尝试允许我的PC IP地址使用gii

我的电脑IP地址为192.168.1.101

server计算机IP为192.168.1.102

我使用composer来安装gii module

这就是我的composer.json设置的样子:

"require": {
        "php": ">=5.4.0",
        "yiisoft/yii2": "*",
        "yiisoft/yii2-bootstrap": "*",
        "yiisoft/yii2-swiftmailer": "*",
        "yiisoft/yii2-gii": "*"
    },
    "require-dev": {
        "yiisoft/yii2-codeception": "*",
        "yiisoft/yii2-debug": "*",
        "yiisoft/yii2-gii": "*",
        "yiisoft/yii2-faker": "*"
    },

我使用了php initcomposer update以及php yii migrate

我也登录了frontend

这是main.php文件内容:

return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['gii'],
    'controllerNamespace' => 'frontend\controllers',
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
    ],
    'params' => $params,
    'modules' => [
        'gii' => [
            'class' => 'yii\gii\Module',
            'allowedIPs' => ['127.0.0.1', '::1', '192.168.1.101'],
            'password' => '123456'
        ],
    ],
];

7 个答案:

答案 0 :(得分:26)

我遇到了类似的问题并尝试了所有不同的ipFilter更改。最后我需要将它添加到main-local.php。这很奇怪,因为我有一个高级应用程序,并且设置是针对' yii2 basic'设置。
http://www.yiiframework.com/doc-2.0/guide-start-gii.html

if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';

$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';}

我还应该指出,我确实将此添加到main.php

    'modules' => [
    'gii' => [
        'class' => 'yii\gii\Module',
        'allowedIPs' => ['127.0.0.1', '::1', '192.168.1.*', 'XXX.XXX.XXX.XXX'] // adjust this to your needs
    ],
],

答案 1 :(得分:12)

dev模式初始化后,我必须更改\backend\config\main-local.php并添加'allowedIPs'。

允许所有 IP,因此仅建议内部开发人员使用! 根据您的需求进行调整。

$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
    'class' => 'yii\gii\Module',
    'allowedIPs' => ['*'],
];

答案 2 :(得分:7)

在当前版本的Yii中,您应该在web.php中执行此操作以允许访问Gii:

//$config['modules']['gii'] = 'yii\gii\Module'; // <--- replace this line
$config['modules']['gii'] = [
    'class' => 'yii\gii\Module',
    'allowedIPs' => ['XXX.XXX.XXX.XXX', 'YYY.YYY.YYY.YYY']
];

答案 3 :(得分:4)

按如下方式更改/common/config/main-local.php:

    return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=YourDatbase',
            'username' => 'YourDBUserName',
            'password' => 'YourDBPassword',
            'charset' => 'utf8',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
    ],
    // Add this to get debug and gii to work
    'modules' => [
        'gii' => [
            'class' => 'yii\gii\Module',
             // permits any and all IPs
             // you should probably restrict this
            'allowedIPs' => ['*']
        ],
        'debug' => [
            'class' => 'yii\debug\Module',
             // permits any and all IPs
             // you should probably restrict this
            'allowedIPs' => ['*']
        ]
    ]
];

答案 4 :(得分:3)

如果部分::

,在YII_ENV_DEV之前添加感叹号(!)后代码对我有用(yii 2.0.8)
if (!YII_ENV_TEST) {
     // configuration adjustments for 'dev' environment
     $config['bootstrap'][] = 'debug';
     $config['modules']['debug'] = [
          'class' => 'yii\debug\Module',
     ];
     $config['modules']['debug']['allowedIPs'] = ['*'];

     $config['bootstrap'][] = 'gii';
     $config['modules']['gii'] = [
          'class' => 'yii\gii\Module',
     ];
     $config['modules']['gii']['allowedIPs'] = ['*'];

 }

答案 5 :(得分:2)

如有疑问,请检查日志。那里有一个警告,应该告诉你像

这样的东西
10  06:00:19.040    warning yii\gii\Module::checkAccess Access to Gii is denied due to IP address restriction. The requested IP is 127.0.0.1
11  06:00:19.041    error   yii\web\HttpException:403   exception 'yii\web\ForbiddenHttpException' with message 'You are not allowed to access this page.' in ......./html/vendor/yiisoft/yii2-gii/Module.php:112

可能你对Ip的看法不对。我刚尝试了你的配置,它对我有用。

PS1:你不应该在服务器上启用Gii,但我认为你已经知道了,这仍然是开发环境。

PS2:Yii2中没有gii的passoword设置

答案 6 :(得分:1)

我找到了答案,这应该由yii团队详细记录!

init使用/frontend/config/main-local.php命令后,我发现:

if (!YII_ENV_TEST) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

我的应用处于dev模式,并且在声明之上,停止我的gii工作,所以...评论该行