GiiModule

CDbHttpSession

Yii Framework v1.1.10 类参考

GiiModule

system.gii
继承 class GiiModule » CWebModule » CModule » CComponent
源自 1.1.2
版本 $Id: GiiModule.php 3426 2011-10-25 00:01:09Z alexander.makarow $
源码 framework/gii/GiiModule.php
GiiModule 是一个模块,它提供基于Web的代码生成功能。

要使用GiiModule,你需要象下面这样将它包含在应用程序的配置文件中:
return array(
    ......
    'modules'=>array(
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>***choose a password***
        ),
    ),
)


因为GuiiModule会在服务器上生成新的代码文件,你应该仅在自己的 进行开发的机器上使用它。为了防止别人使用此模块,它需要你在配 置文件中指定一个安全密码。当你通过浏览器访问GiiModule时,你 将被提示需要输入正确的密码。

默认情况下,GiiModule只能通过本地主机访问。 如果你想在其他机 器上访问,可以配置其 ipFilters属性以实现此目的。

通过上面的配置,你就可以访问GiiModule了。在你的浏览器中访问此 URL:

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

如果您的应用程序是使用一些自定义URL规则的路径格式的URL, 您在您的应用程序配置中添加以下URL以访问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...
        ),
    )
)


现在你可以通过下面的URL访问到GiiModule:

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

公共属性

隐藏继承属性

属性类型描述定义在
assetsUrl string 包含了Gii的所有资源文件(asset files)的基准URL。 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
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 一个路径别名的列表,它们指向包含各种代码生成器的 目录。一个单一的路径别名指向的目录可以包含多个代码生成器,每 个代码生成器存放在以生成器名字命名的子目录中。 默认值是('application. GiiModule
id string 返回模块ID。 CModule
ipFilters array IP过滤器的列表,指定哪些IP允许访问GiiModule。 每个数组元素表达一个单一的过滤器。一个过滤器既可以是一个 IP地址,也可以是一个含有通配符的地址(例如:192. 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 返回包含应用程序模块的目录。 CModule
modules array 返回应用程序当前已经安装的模块。 CModule
name string Returns the name of this module. CWebModule
newDirMode integer 为新生成的目录设置的权限。 这个值将被PHPchmod函数使用。 默认值是0777,表示此文件对所有用户都是可读、写、执行的。 GiiModule
newFileMode integer 为新生成的代码文件设置的权限。 这个值将被PHPchmod函数使用。 默认值是0666,表示此文件对所有用户都是可读写的。 GiiModule
params CAttributeCollection 返回用户定义的参数。 CModule
parentModule CModule 返回父模块。 CModule
password string 用于访问GiiModule的密码。 如果设置为false,将可以不使用密码访问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() 如果类中没有调的方法名,则调用这个方法。 CComponent
__construct() 构造方法。 CModule
__get() getter魔术方法。 CModule
__isset() 检查一个属性值是否为null。 CModule
__set() 设置一个组件的属性值。 CComponent
__unset() 设置一个组件的属性为null。 CComponent
afterControllerAction() The post-filter for controller actions. CWebModule
asa() 返回这个名字的行为对象。 CComponent
attachBehavior() 附加一个行为到组件。 CComponent
attachBehaviors() 附加一个行为列表到组件。 CComponent
attachEventHandler() 为事件附加一个事件处理程序。 CComponent
beforeControllerAction() 执行对Gii的访问检查。 GiiModule
canGetProperty() 确定属性是否可读。 CComponent
canSetProperty() 确定属性是否可写。 CComponent
configure() 为模块指定配置。 CModule
detachBehavior() 从组件中分离一个行为。 CComponent
detachBehaviors() 从组件中分离所有行为。 CComponent
detachEventHandler() 分离一个存在的事件处理程序。 CComponent
disableBehavior() 禁用一个附加行为。 CComponent
disableBehaviors() 禁用组件附加的所有行为。 CComponent
enableBehavior() 启用一个附加行为。 CComponent
enableBehaviors() 启用组件附加的所有行为。 CComponent
evaluateExpression() 计算一个PHP表达式,或根据组件上下文执行回调。 CComponent
getAssetsUrl() 返回包含了Gii的所有资源文件(asset files)的基准URL。 GiiModule
getBasePath() 返回模块根目录。 CModule
getComponent() Retrieves the named application component. CModule
getComponents() 返回应用组件。 CModule
getControllerPath() 返回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() 返回一个事件的附加处理程序列表。 CComponent
getId() 返回模块ID。 CModule
getLayoutPath() 返回the root directory of layout files. Defaults to 'moduleDir/views/layouts' where moduleDir is the directory containing the module class. CWebModule
getModule() 检索指定的应用模块。 CModule
getModulePath() 返回包含应用程序模块的目录。 CModule
getModules() 返回应用程序当前已经安装的模块。 CModule
getName() Returns the name of this module. CWebModule
getParams() 返回用户定义的参数。 CModule
getParentModule() 返回父模块。 CModule
getVersion() Returns the version of this module. CWebModule
getViewPath() 返回the root directory of view files. Defaults to 'moduleDir/views' where moduleDir is the directory containing the module class. CWebModule
hasComponent() 检查是否存在指定组件。 CModule
hasEvent() 确定一个事件是否定义。 CComponent
hasEventHandler() 检查事件是否有附加的处理程序。 CComponent
hasModule() 检查是否已经安装了指定模块。 CModule
hasProperty() 确定属性是否被定义。 CComponent
init() 初始化gii模块. GiiModule
raiseEvent() 发起一个事件。 CComponent
setAliases() 定义根目录的别名。 CModule
setAssetsUrl() 设置包含了Gii的所有资源文件(asset files)的基准URL。 GiiModule
setBasePath() 设置块根目录。 CModule
setComponent() 添加一个组件到模块中 CModule
setComponents() 设置应用组件。 CModule
setControllerPath() 设置the directory that contains the controller classes. CWebModule
setId() 设置模块ID。 CModule
setImport() 设置在模块中使用的路径别名。 CModule
setLayoutPath() 设置the root directory of layout files. CWebModule
setModulePath() 设置包含应用程序模块的目录。 CModule
setModules() 配置模块的子模块。 CModule
setParams() 设置用户定义的参数。 CModule
setViewPath() 设置the root directory of view files. CWebModule

受保护方法

隐藏继承方法

方法描述定义在
allowIp() 检查用户的来源IP是否被ipFilters允许。 GiiModule
findGenerators() 查找所有可用的代码生成器和他们的代码模板。 GiiModule
preinit() 模块预初始化。 CModule
preloadComponents() 加载静态应用组件。 CModule

属性详细

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

包含了Gii的所有资源文件(asset files)的基准URL。

generatorPaths 属性
public array $generatorPaths;

一个路径别名的列表,它们指向包含各种代码生成器的 目录。一个单一的路径别名指向的目录可以包含多个代码生成器,每 个代码生成器存放在以生成器名字命名的子目录中。 默认值是('application.gii')。

ipFilters 属性
public array $ipFilters;

IP过滤器的列表,指定哪些IP允许访问GiiModule。 每个数组元素表达一个单一的过滤器。一个过滤器既可以是一个 IP地址,也可以是一个含有通配符的地址(例如:192.168.0.*), 后者表示一个网段。如果你希望允许所有的ID访问GiiModule,你 需要将此值设置为false(除非你明确后果否则请别这么做!!!) 默认值是 array('127.0.0.1', '::1'),表示只有本地主机才可以 访问GiiModule。

newDirMode 属性
public integer $newDirMode;

为新生成的目录设置的权限。 这个值将被PHPchmod函数使用。 默认值是0777,表示此文件对所有用户都是可读、写、执行的。

newFileMode 属性
public integer $newFileMode;

为新生成的代码文件设置的权限。 这个值将被PHPchmod函数使用。 默认值是0666,表示此文件对所有用户都是可读写的。

password 属性
public string $password;

用于访问GiiModule的密码。 如果设置为false,将可以不使用密码访问GiiModule。 (注意:除非你明确知道后果,否则请不要如此做!!!)

方法详细

allowIp() 方法
protected boolean allowIp(string $ip)
$ip string 用户IP
{return} boolean 用户的来源IP是否被ipFilters允许。
源码: framework/gii/GiiModule.php#184 (显示)
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;
}

检查用户的来源IP是否被ipFilters允许。

beforeControllerAction() 方法
public boolean beforeControllerAction(CController $controller, CAction $action)
$controller CController 需要访问的控制器。
$action CAction 需要访问的动作。
{return} boolean 此动作是否可以执行。
源码: framework/gii/GiiModule.php#159 (显示)
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;
}

执行对Gii的访问检查。 此方法当用户访问除了"default/login"和"default/error"之外的动作时, 检查用户的来源IP和密码是否正确。

findGenerators() 方法
protected array findGenerators()
{return} array
源码: framework/gii/GiiModule.php#200 (显示)
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;
}

查找所有可用的代码生成器和他们的代码模板。

getAssetsUrl() 方法
public string getAssetsUrl()
{return} string 包含了Gii的所有资源文件(asset files)的基准URL。
源码: framework/gii/GiiModule.php#136 (显示)
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#115 (显示)
public function init()
{
    
parent::init();
    
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'),
        ),
    ), 
false);
    
$this->generatorPaths[]='gii.generators';
    
$this->controllerMap=$this->findGenerators();
}

初始化gii模块.

setAssetsUrl() 方法
public void setAssetsUrl(string $value)
$value string 包含了Gii的所有资源文件(asset files)的基准URL。
源码: framework/gii/GiiModule.php#146 (显示)
public function setAssetsUrl($value)
{
    
$this->_assetsUrl=$value;
}

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.