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'
        ],
    ],
];
Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100

8 Answers8

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
    ],
],
johnsnails
  • 1,971
  • 20
  • 29
  • see my answer below, adding 'allowedIPs' in main-local.php should be enough – mfgmicha Feb 03 '16 at 11:16
  • 2
    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 `allowedIPs` – Ripper Jan 10 '17 at 10:44
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' => ['*'],
];
mfgmicha
  • 3,784
  • 1
  • 25
  • 26
10

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' => ['*']
        ]
    ]
];
Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100
user2161402
  • 111
  • 1
  • 3
  • Thanks, I was struggling with this when trying to access gii while running development server on docker – Hamfri Jun 30 '21 at 14:59
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']
];
Doychin Dokov
  • 79
  • 1
  • 1
  • Correct! In Yii 2.0 the settings for allowed IP must be included in web.php instead of console.php – andreapavan May 01 '15 at 16:01
  • 1
    That's assuming you are using the Yii2 Basic, slightly different for Yii2 Advanced Template. There is no web.php in the Advanced Template. – johnsnails Oct 07 '15 at 23:03
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'] = ['*'];

 }
RAPOS
  • 99
  • 1
  • 2
  • 10
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

Mihai P.
  • 9,307
  • 3
  • 38
  • 49
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

Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100
-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('*'),
    ),
Xcoder
  • 266
  • 1
  • 5
  • 13