Versions
Look up a class, method, property or event

GiiModule

Package system.gii
Inheritance class GiiModule » CWebModule » CModule » CComponent
Since 1.1.2
Source Code framework/gii/GiiModule.php
GiiModule is a module that provides Web-based code generation capabilities.

To use GiiModule, you must include it as a module in the application configuration like the following:
return array(
    ......
    'modules'=>array(
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>***choose a password***
        ),
    ),
)


Because GiiModule 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, it is required that you specify a secret password in the configuration. Later when you access the module via browser, you will be prompted to enter the correct password.

By default, GiiModule can only be accessed by localhost. You may configure its ipFilters 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 following URL:

http://localhost/path/to/index.php?r=gii

If your application is using path-format URLs with some customized URL rules, you may need to add the following URLs in your application configuration in order to access GiiModule:
'components'=>array(
    'urlManager'=>array(
        'urlFormat'=>'path',
        'rules'=>array(
            'gii'=>'gii',
            'gii/<controller:\w+>'=>'gii/<controller>',
            'gii/<controller:\w+>/<action:\w+>'=>'gii/<controller>/<action>',
            ...other rules...
        ),
    )
)


You can then access GiiModule via:

http://localhost/path/to/index.php/gii

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
assetsUrl string the base URL that contains all published asset files of gii. GiiModule
basePath string Returns the root directory of the module. CModule
behaviors array the behaviors that should be attached to the module. CModule
components array Returns the application components. CModule
controllerMap array mapping from controller ID to controller configurations. CWebModule
controllerNamespace string Namespace that should be used when loading controllers. CWebModule
controllerPath string the directory that contains the controller classes. CWebModule
defaultController string the ID of the default controller for this module. CWebModule
description string Returns the description of this module. CWebModule
generatorPaths array a list of path aliases that refer to the directories containing code generators. GiiModule
id string Returns the module ID. CModule
ipFilters array the IP filters that specify which IP addresses are allowed to access GiiModule. GiiModule
layout mixed the layout that is shared by the controllers inside this module. CWebModule
layoutPath string the root directory of layout files. CWebModule
modulePath string Returns the directory that contains the application modules. CModule
modules array Returns the configuration of the currently installed modules. CModule
name string Returns the name of this module. CWebModule
newDirMode integer the permission to be set for newly generated directories. GiiModule
newFileMode integer the permission to be set for newly generated code files. GiiModule
params CAttributeCollection Returns user-defined parameters. CModule
parentModule CModule Returns the parent module. CModule
password string the password that can be used to access GiiModule. GiiModule
preload array the IDs of the application components that should be preloaded. CModule
version string Returns the version of this module. CWebModule
viewPath string the root directory of view files. CWebModule

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CModule
__get() Getter magic method. CModule
__isset() Checks if a property value is null. CModule
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
afterControllerAction() The post-filter for controller actions. CWebModule
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
beforeControllerAction() Performs access check to gii. GiiModule
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
configure() Configures the module with the specified configuration. CModule
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getAssetsUrl() Returns the base URL that contains all published asset files of gii. GiiModule
getBasePath() Returns the root directory of the module. CModule
getComponent() Retrieves the named application component. CModule
getComponents() Returns the application components. CModule
getControllerPath() Returns the directory that contains the controller classes. Defaults to 'moduleDir/controllers' where moduleDir is the directory containing the module class. CWebModule
getDescription() Returns the description of this module. CWebModule
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getId() Returns the module ID. CModule
getLayoutPath() Returns the root directory of layout files. Defaults to 'moduleDir/views/layouts' where moduleDir is the directory containing the module class. CWebModule
getModule() Retrieves the named application module. CModule
getModulePath() Returns the directory that contains the application modules. CModule
getModules() Returns the configuration of the currently installed modules. CModule
getName() Returns the name of this module. CWebModule
getParams() Returns user-defined parameters. CModule
getParentModule() Returns the parent module. CModule
getVersion() Returns the version of this module. CWebModule
getViewPath() Returns the root directory of view files. Defaults to 'moduleDir/views' where moduleDir is the directory containing the module class. CWebModule
hasComponent() Checks whether the named component exists. CModule
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasModule() Returns a value indicating whether the specified module is installed. CModule
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the gii module. GiiModule
raiseEvent() Raises an event. CComponent
setAliases() Defines the root aliases. CModule
setAssetsUrl() Sets the base URL that contains all published asset files of gii. GiiModule
setBasePath() Sets the root directory of the module. CModule
setComponent() Puts a component under the management of the module. CModule
setComponents() Sets the application components. CModule
setControllerPath() Sets the directory that contains the controller classes. CWebModule
setId() Sets the module ID. CModule
setImport() Sets the aliases that are used in the module. CModule
setLayoutPath() Sets the root directory of layout files. CWebModule
setModulePath() Sets the directory that contains the application modules. CModule
setModules() Configures the sub-modules of this module. CModule
setParams() Sets user-defined parameters. CModule
setViewPath() Sets the root directory of view files. CWebModule

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
allowIp() Checks to see if the user IP is allowed by ipFilters. GiiModule
findGenerators() Finds all available code generators and their code templates. GiiModule
preinit() Preinitializes the module. CModule
preloadComponents() Loads static application components. CModule

Property Details

assetsUrl property
public string getAssetsUrl()
public void setAssetsUrl(string $value)

the base URL that contains all published asset files of gii.

generatorPaths property
public array $generatorPaths;

a list of path aliases that refer to the directories containing code generators. The directory referred by a single path alias may contain multiple code generators, each stored under a sub-directory whose name is the generator name. Defaults to array('application.gii').

ipFilters property
public array $ipFilters;

the IP filters that specify which IP addresses are allowed to access GiiModule. Each array element represents a single filter. A filter can be either an IP address or an address with wildcard (e.g. 192.168.0.*) to represent a network segment. If you want to allow all IPs to access gii, you may set this property to be false (DO NOT DO THIS UNLESS YOU KNOW THE CONSEQUENCE!!!) The default value is array('127.0.0.1', '::1'), which means GiiModule can only be accessed on the localhost.

newDirMode property
public integer $newDirMode;

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.

newFileMode property
public integer $newFileMode;

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.

password property
public string $password;

the password that can be used to access GiiModule. If this property is set false, then GiiModule can be accessed without password (DO NOT DO THIS UNLESS YOU KNOW THE CONSEQUENCE!!!)

Method Details

allowIp() method
protected boolean allowIp(string $ip)
$ip string the user IP
{return} boolean whether the user IP is allowed by ipFilters.
Source Code: framework/gii/GiiModule.php#189 (show)
protected function allowIp($ip)
{
    if(empty(
$this->ipFilters))
        return 
true;
    foreach(
$this->ipFilters as $filter)
    {
        if(
$filter==='*' || $filter===$ip || (($pos=strpos($filter,'*'))!==false && !strncmp($ip,$filter,$pos)))
            return 
true;
    }
    return 
false;
}

Checks to see if the user IP is allowed by ipFilters.

beforeControllerAction() method
public boolean beforeControllerAction(CController $controller, CAction $action)
$controller CController the controller to be accessed.
$action CAction the action to be accessed.
{return} boolean whether the action should be executed.
Source Code: framework/gii/GiiModule.php#164 (show)
public function beforeControllerAction($controller$action)
{
    if(
parent::beforeControllerAction($controller$action))
    {
        
$route=$controller->id.'/'.$action->id;
        if(!
$this->allowIp(Yii::app()->request->userHostAddress) && $route!=='default/error')
            throw new 
CHttpException(403,"You are not allowed to access this page.");

        
$publicPages=array(
            
'default/login',
            
'default/error',
        );
        if(
$this->password!==false && Yii::app()->user->isGuest && !in_array($route,$publicPages))
            
Yii::app()->user->loginRequired();
        else
            return 
true;
    }
    return 
false;
}

Performs access check to gii. This method will check to see if user IP and password are correct if they attempt to access actions other than "default/login" and "default/error".

findGenerators() method
protected array findGenerators()
{return} array
Source Code: framework/gii/GiiModule.php#205 (show)
protected function findGenerators()
{
    
$generators=array();
    
$n=count($this->generatorPaths);
    for(
$i=$n-1;$i>=0;--$i)
    {
        
$alias=$this->generatorPaths[$i];
        
$path=Yii::getPathOfAlias($alias);
        if(
$path===false || !is_dir($path))
            continue;

        
$names=scandir($path);
        foreach(
$names as $name)
        {
            if(
$name[0]!=='.' && is_dir($path.'/'.$name))
            {
                
$className=ucfirst($name).'Generator';
                if(
is_file("$path/$name/$className.php"))
                {
                    
$generators[$name]=array(
                        
'class'=>"$alias.$name.$className",
                    );
                }

                if(isset(
$generators[$name]) && is_dir("$path/$name/templates"))
                {
                    
$templatePath="$path/$name/templates";
                    
$dirs=scandir($templatePath);
                    foreach(
$dirs as $dir)
                    {
                        if(
$dir[0]!=='.' && is_dir($templatePath.'/'.$dir))
                            
$generators[$name]['templates'][$dir]=strtr($templatePath.'/'.$dir,array('/'=>DIRECTORY_SEPARATOR,'\\'=>DIRECTORY_SEPARATOR));
                    }
                }
            }
        }
    }
    return 
$generators;
}

Finds all available code generators and their code templates.

getAssetsUrl() method
public string getAssetsUrl()
{return} string the base URL that contains all published asset files of gii.
Source Code: framework/gii/GiiModule.php#140 (show)
public function getAssetsUrl()
{
    if(
$this->_assetsUrl===null)
        
$this->_assetsUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('gii.assets'));
    return 
$this->_assetsUrl;
}

init() method
public void init()
Source Code: framework/gii/GiiModule.php#114 (show)
public function init()
{
    
parent::init();
    
Yii::setPathOfAlias('gii',dirname(__FILE__));
    
Yii::app()->setComponents(array(
        
'errorHandler'=>array(
            
'class'=>'CErrorHandler',
            
'errorAction'=>$this->getId().'/default/error',
        ),
        
'user'=>array(
            
'class'=>'CWebUser',
            
'stateKeyPrefix'=>'gii',
            
'loginUrl'=>Yii::app()->createUrl($this->getId().'/default/login'),
        ),
        
'widgetFactory' => array(
            
'class'=>'CWidgetFactory',
            
'widgets' => array()
        )
    ), 
false);
    
$this->generatorPaths[]='gii.generators';
    
$this->controllerMap=$this->findGenerators();
}

Initializes the gii module.

setAssetsUrl() method
public void setAssetsUrl(string $value)
$value string the base URL that contains all published asset files of gii.
Source Code: framework/gii/GiiModule.php#150 (show)
public function setAssetsUrl($value)
{
    
$this->_assetsUrl=$value;
}

Total 1 comment

#5781 report it
Don Felipe at 2011/11/13 02:29am
Use your own custom templates for CRUD generations

I couldn't find any information about this so it's worth sharing the info. Depending on client and project the webapp may use different CSS Frameworks and therefore need to use different HTML code fragments (for input forms for instance) thus making the default HTML code generated by GII useless or at least very painful to adjust manually. But it doesn't have to be that way!

You can simply create your own HTML templates and put it under

/gii/generators/crud/templates/

You can duplicate an existing one - first time Gii's default folder and codes - and easily adjust the new template and name it after the webapp it is meant for or the CSS Framework; e.g. "bootstrap".

Then in GII's interface and under "Crud Generator", and that is the tricky part to figure out!, one has to click onto "default..." under Code Template and a drop down list magically appears and will show your additional template folders. E Voila. Luckily it remembers your selection throughout the browser session.

Leave a comment

Please to leave your comment.