Class yii\gii\Module

Inheritanceyii\gii\Module » yii\base\Module
Implementsyii\base\BootstrapInterface
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-gii/blob/master/src/Module.php

This is the main module class for the Gii module.

To use Gii, include it as a module in the application configuration like the following:

return [
    'bootstrap' => ['gii'],
    'modules' => [
        'gii' => ['class' => 'yii\gii\Module'],
    ],
]

Because Gii generates new code files on the server, you should only use it on your own development machine. To prevent other people from using this module, by default, Gii can only be accessed by localhost. You may configure its $allowedIPs property if you want to make it accessible on other machines.

With the above configuration, you will be able to access GiiModule in your browser using the URL http://localhost/path/to/index.php?r=gii

If your application enables \yii\web\UrlManager::enablePrettyUrl, you can then access Gii via URL: http://localhost/path/to/index.php/gii

Public Properties

Hide inherited properties

Property Type Description Defined By
$allowedIPs array The list of IPs that are allowed to access this module. yii\gii\Module
$controllerNamespace yii\gii\Module
$generators array|yii\gii\Generator[] A list of generator configurations or instances. yii\gii\Module
$newDirMode integer The permission to be set for newly generated directories. yii\gii\Module
$newFileMode integer The permission to be set for newly generated code files. yii\gii\Module

Public Methods

Hide inherited methods

Method Description Defined By
beforeAction() yii\gii\Module
bootstrap() yii\gii\Module

Protected Methods

Hide inherited methods

Method Description Defined By
checkAccess() yii\gii\Module
coreGenerators() Returns the list of the core code generator configurations. yii\gii\Module
defaultVersion() yii\gii\Module
resetGlobalSettings() Resets potentially incompatible global settings done in app config. yii\gii\Module

Property Details

Hide inherited properties

$allowedIPs public property

The list of IPs that are allowed to access this module. Each array element represents a single IP filter which can be either an IP address or an address with wildcard (e.g. 192.168.0.*) to represent a network segment. The default value is ['127.0.0.1', '::1'], which means the module can only be accessed by localhost.

public array $allowedIPs = [
    
'127.0.0.1',
    
'::1',
]
$controllerNamespace public property
public $controllerNamespace 'yii\gii\controllers'
$generators public property

A list of generator configurations or instances. The array keys are the generator IDs (e.g. "crud"), and the array elements are the corresponding generator configurations or the instances.

After the module is initialized, this property will become an array of generator instances which are created based on the configurations previously taken by this property.

Newly assigned generators will be merged with the core ones, and the former takes precedence in case when they have the same generator ID.

$newDirMode public property

The permission to be set for newly generated directories. This value will be used by PHP chmod function. Defaults to 0777, meaning the directory can be read, written and executed by all users.

public integer $newDirMode 0777
$newFileMode public property

The permission to be set for newly generated code files. This value will be used by PHP chmod function. Defaults to 0666, meaning the file is read-writable by all users.

public integer $newFileMode 0666

Method Details

Hide inherited methods

beforeAction() public method

public void beforeAction ( $action )
$action

                public function beforeAction($action)
{
    if (!parent::beforeAction($action)) {
        return false;
    }
    if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess()) {
        throw new ForbiddenHttpException('You are not allowed to access this page.');
    }
    foreach (array_merge($this->coreGenerators(), $this->generators) as $id => $config) {
        if (is_object($config)) {
            $this->generators[$id] = $config;
        } else {
            $this->generators[$id] = Yii::createObject($config);
        }
    }
    $this->resetGlobalSettings();
    return true;
}

            
bootstrap() public method

public void bootstrap ( $app )
$app

                public function bootstrap($app)
{
    if ($app instanceof \yii\web\Application) {
        $app->getUrlManager()->addRules([
            ['class' => 'yii\web\UrlRule', 'pattern' => $this->id, 'route' => $this->id . '/default/index'],
            ['class' => 'yii\web\UrlRule', 'pattern' => $this->id . '/<id:\w+>', 'route' => $this->id . '/default/view'],
            ['class' => 'yii\web\UrlRule', 'pattern' => $this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>', 'route' => $this->id . '/<controller>/<action>'],
        ], false);
    } elseif ($app instanceof \yii\console\Application) {
        $app->controllerMap[$this->id] = [
            'class' => 'yii\gii\console\GenerateController',
            'generators' => array_merge($this->coreGenerators(), $this->generators),
            'module' => $this,
        ];
    }
}

            
checkAccess() protected method

protected integer checkAccess ( )
return integer

Whether the module can be accessed by the current user

                protected function checkAccess()
{
    $ip = Yii::$app->getRequest()->getUserIP();
    foreach ($this->allowedIPs as $filter) {
        if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
            return true;
        }
    }
    Yii::warning('Access to Gii is denied due to IP address restriction. The requested IP is ' . $ip, __METHOD__);
    return false;
}

            
coreGenerators() protected method

Returns the list of the core code generator configurations.

protected array coreGenerators ( )
return array

The list of the core code generator configurations.

                protected function coreGenerators()
{
    return [
        'model' => ['class' => 'yii\gii\generators\model\Generator'],
        'crud' => ['class' => 'yii\gii\generators\crud\Generator'],
        'controller' => ['class' => 'yii\gii\generators\controller\Generator'],
        'form' => ['class' => 'yii\gii\generators\form\Generator'],
        'module' => ['class' => 'yii\gii\generators\module\Generator'],
        'extension' => ['class' => 'yii\gii\generators\extension\Generator'],
    ];
}

            
defaultVersion() protected method (available since version 2.0.6)

protected void defaultVersion ( )

                protected function defaultVersion()
{
    $packageInfo = Json::decode(file_get_contents(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'composer.json'));
    $extensionName = $packageInfo['name'];
    if (isset(Yii::$app->extensions[$extensionName])) {
        return Yii::$app->extensions[$extensionName]['version'];
    }
    return parent::defaultVersion();
}

            
resetGlobalSettings() protected method

Resets potentially incompatible global settings done in app config.

protected void resetGlobalSettings ( )

                protected function resetGlobalSettings()
{
    if (Yii::$app instanceof \yii\web\Application) {
        Yii::$app->assetManager->bundles = [];
    }
}