Yii2 Gii Forbidden code 403 You are not allowed to access this page
Asked Answered
G

8

21

I have a server machine and I am trying to allow my PC ip address to use gii.

My PC ip address is 192.168.1.101

The server machine ip is 192.168.1.102.

I used composer to install the gii module.

This is how my composer.json settings look like:

"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": "*"
    },

I have used php init and composer update and php yii migrate.

I am also logged in in the frontend.

This is the main.php file content:

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'
        ],
    ],
];
Gunstock answered 11/10, 2014 at 14:38 Comment(0)
S
35

I had a similar issue and tried all different ipFilter changes. In the end I needed to add this to main-local.php. Which was strange because I had an advanced application, and the settings were for a 'yii2 basic' setup.
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';}

I should also point out, I did add this to 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
    ],
],
Shouldst answered 6/11, 2014 at 11:52 Comment(2)
see my answer below, adding 'allowedIPs' in main-local.php should be enoughProffitt
I had simmilar problem when I uploaded application to production server, looks like if you do not specify allowedIPs in $config['modules']['debug'] = [ it defaults to localhost, so I was unable to access debug on production, solved by adding allowedIPsLolitaloll
P
20

After init in dev mode I had to change my \backend\config\main-local.php and add the 'allowedIPs'.

Allows ALL IPs, so only recommended for internal dev use! Adjust to your needs.

$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
    'class' => 'yii\gii\Module',
    'allowedIPs' => ['*'],
];
Proffitt answered 3/2, 2016 at 11:6 Comment(0)
E
11

Change your /common/config/main-local.php as follows:

    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' => ['*']
        ]
    ]
];
Embodiment answered 25/3, 2016 at 17:27 Comment(1)
Thanks, I was struggling with this when trying to access gii while running development server on dockerKnowable
C
7

In the current version of Yii, you should do that in web.php to allow access to 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']
];
Commander answered 16/4, 2015 at 18:27 Comment(2)
Correct! In Yii 2.0 the settings for allowed IP must be included in web.php instead of console.phpGowen
That's assuming you are using the Yii2 Basic, slightly different for Yii2 Advanced Template. There is no web.php in the Advanced Template.Shouldst
H
5

The code worked for me(yii 2.0.8) after adding a exclamation mark(!) before YII_ENV_DEV inside if part::

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'] = ['*'];

 }
Humic answered 9/10, 2017 at 17:38 Comment(1)
Thanks, Just your code worked for me.Vowell
B
2

When in doubt check the logs. There is a warning in there that should tell you something like

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

Probably you are wrong about the Ip. I just tried the configuration you have and it works for me.

PS1: You should not have Gii enabled on a server but I assume you know that already and this is still the development environment.

PS2: there is no passoword setting for gii in Yii2

Beirut answered 13/10, 2014 at 6:8 Comment(0)
G
1

I found the answer, and this should be well documented by the yii team!

After I used the init command, in /frontend/config/main-local.php, I found:

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';
}

My app is in dev mode, and te above declaration, stops my gii to work, so ... comment that line

Gunstock answered 7/11, 2014 at 19:42 Comment(0)
A
-1

I had to add this to my module configurations

'gii' => array(
        'generatorPaths' => array('bootstrap.gii'),
        'class' => 'system.gii.GiiModule',
        'password' => 'aaa123',
        // If removed, Gii defaults to localhost only. Edit carefully to taste.
        'ipFilters' => array('*'),
    ),
Anemic answered 21/5, 2018 at 21:29 Comment(1)
Your answer is for Yii1, question is about Yii2.Tuning

© 2022 - 2024 — McMap. All rights reserved.