system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.cubrid system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.gii system.i18n system.i18n.gettext system.logging system.test system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.form system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers zii.behaviors zii.widgets zii.widgets.grid zii.widgets.jui

GiiModule

system.gii
继承 class GiiModule » CWebModule » CModule » CComponent
可用自 1.1.2
源码 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

公共属性

隐藏继承的属性

属性类型描述被定义在
assetsUrl string the base URL that contains all published asset files of gii. GiiModule
basePath string 返回模块的根目录。 CModule
behaviors array the behaviors that should be attached to the module. CModule
components array 返回应用程序组件。 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 返回模块的 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 返回用户定义的参数。 CModule
parentModule CModule 返回父模块。 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

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. CComponent
__construct() 构造器。 CModule
__get() Getter 魔术方法。 CModule
__isset() 检查一个属性值是否为空。 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() 返回模块的根目录。 CModule
getComponent() Retrieves the named application component. CModule
getComponents() 返回应用程序组件。 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() 返回模块的 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() 检索指定的应用程序模块。 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() 返回用户定义的参数。 CModule
getParentModule() 返回父模块。 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() 设置模块的根目录。 CModule
setComponent() Puts a component under the management of the module. CModule
setComponents() 设置应用程序组件。 CModule
setControllerPath() Sets the directory that contains the controller classes. CWebModule
setId() 设置模块的 ID。 CModule
setImport() 设置用于在模块中使用的别名。 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() 设置用户定义的参数 CModule
setViewPath() Sets the root directory of view files. CWebModule

受保护的方法

隐藏继承的方法

方法描述被定义在
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() 加载静态应用程序组件。 CModule

属性详情

assetsUrl 属性
public string getAssetsUrl()
public void setAssetsUrl(string $value)

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

generatorPaths 属性
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 属性
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 属性
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 属性
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 属性
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!!!)

方法详情

allowIp() 方法
protected boolean allowIp(string $ip)
$ip string the user IP
{return} boolean whether the user IP is allowed by ipFilters.
源码: framework/gii/GiiModule.php#189 (显示)
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() 方法
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.
源码: framework/gii/GiiModule.php#164 (显示)
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() 方法
protected array findGenerators()
{return} array
源码: framework/gii/GiiModule.php#205 (显示)
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() 方法
public string getAssetsUrl()
{return} string the base URL that contains all published asset files of gii.
源码: framework/gii/GiiModule.php#140 (显示)
public function getAssetsUrl()
{
    if(
$this->_assetsUrl===null)
        
$this->_assetsUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('gii.assets'));
    return 
$this->_assetsUrl;
}

init() 方法
public void init()
源码: framework/gii/GiiModule.php#114 (显示)
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() 方法
public void setAssetsUrl(string $value)
$value string the base URL that contains all published asset files of gii.
源码: framework/gii/GiiModule.php#150 (显示)
public function setAssetsUrl($value)
{
    
$this->_assetsUrl=$value;
}