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

CModule

system.base
继承 abstract class CModule » CComponent
子类 CApplication, CWebModule
源码 framework/base/CModule.php
CModule is the base class for module and application classes.

CModule mainly manages application components and sub-modules.

公共属性

隐藏继承的属性

属性类型描述被定义在
basePath string 返回模块的根目录。 CModule
behaviors array the behaviors that should be attached to the module. CModule
components array 返回应用程序组件。 CModule
id string 返回模块的 ID。 CModule
modulePath string Returns the directory that contains the application modules. CModule
modules array Returns the configuration of the currently installed modules. CModule
params CAttributeCollection 返回用户定义的参数。 CModule
parentModule CModule 返回父模块。 CModule
preload array the IDs of the application components that should be preloaded. CModule

公共方法

隐藏继承的方法

方法描述被定义在
__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
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
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
getBasePath() 返回模块的根目录。 CModule
getComponent() Retrieves the named application component. CModule
getComponents() 返回应用程序组件。 CModule
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getId() 返回模块的 ID。 CModule
getModule() 检索指定的应用程序模块。 CModule
getModulePath() Returns the directory that contains the application modules. CModule
getModules() Returns the configuration of the currently installed modules. CModule
getParams() 返回用户定义的参数。 CModule
getParentModule() 返回父模块。 CModule
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
raiseEvent() Raises an event. CComponent
setAliases() Defines the root aliases. CModule
setBasePath() 设置模块的根目录。 CModule
setComponent() Puts a component under the management of the module. CModule
setComponents() 设置应用程序组件。 CModule
setId() 设置模块的 ID。 CModule
setImport() 设置用于在模块中使用的别名。 CModule
setModulePath() Sets the directory that contains the application modules. CModule
setModules() Configures the sub-modules of this module. CModule
setParams() 设置用户定义的参数 CModule

受保护的方法

隐藏继承的方法

方法描述被定义在
init() 初始化模块。 CModule
preinit() Preinitializes the module. CModule
preloadComponents() 加载静态应用程序组件。 CModule

属性详情

basePath 属性
public string getBasePath()
public void setBasePath(string $path)

返回模块的根目录。

behaviors 属性
public array $behaviors;

the behaviors that should be attached to the module. The behaviors will be attached to the module when init is called. Please refer to CModel::behaviors on how to specify the value of this property.

components 属性
public array getComponents(boolean $loadedOnly=true)
public void setComponents(array $components, boolean $merge=true)

返回应用程序组件。

id 属性
public string getId()
public void setId(string $id)

返回模块的 ID。

modulePath 属性
public string getModulePath()
public void setModulePath(string $value)

Returns the directory that contains the application modules.

modules 属性
public array getModules()
public void setModules(array $modules, boolean $merge=true)

Returns the configuration of the currently installed modules.

params 属性
public CAttributeCollection getParams()
public void setParams(array $value)

返回用户定义的参数。

parentModule 属性 只读
public CModule getParentModule()

返回父模块。

preload 属性
public array $preload;

the IDs of the application components that should be preloaded.

方法详情

__construct() 方法
public void __construct(string $id, CModule $parent, mixed $config=NULL)
$id string 模块的 ID
$parent CModule 父模块(如果有)
$config mixed 模块配置。It can be either an array or the path of a PHP file returning the configuration array.
源码: framework/base/CModule.php#69 (显示)
public function __construct($id,$parent,$config=null)
{
    
$this->_id=$id;
    
$this->_parentModule=$parent;

    
// set basePath as early as possible to avoid trouble
    
if(is_string($config))
        
$config=require($config);
    if(isset(
$config['basePath']))
    {
        
$this->setBasePath($config['basePath']);
        unset(
$config['basePath']);
    }
    
Yii::setPathOfAlias($id,$this->getBasePath());

    
$this->preinit();

    
$this->configure($config);
    
$this->attachBehaviors($this->behaviors);
    
$this->preloadComponents();

    
$this->init();
}

构造器。

__get() 方法
public mixed __get(string $name)
$name string 应用组件或属性名称
{return} mixed 指定的属性值
源码: framework/base/CModule.php#100 (显示)
public function __get($name)
{
    if(
$this->hasComponent($name))
        return 
$this->getComponent($name);
    else
        return 
parent::__get($name);
}

Getter 魔术方法。 This method is overridden to support accessing application components like reading module properties.

__isset() 方法
public boolean __isset(string $name)
$name string 属性名称或事件名称
{return} boolean 属性值是否为空
源码: framework/base/CModule.php#115 (显示)
public function __isset($name)
{
    if(
$this->hasComponent($name))
        return 
$this->getComponent($name)!==null;
    else
        return 
parent::__isset($name);
}

检查一个属性值是否为空。 This method overrides the parent implementation by checking if the named application component is loaded.

configure() 方法
public void configure(array $config)
$config array 配置数组
源码: framework/base/CModule.php#515 (显示)
public function configure($config)
{
    if(
is_array($config))
    {
        foreach(
$config as $key=>$value)
            
$this->$key=$value;
    }
}

Configures the module with the specified configuration.

getBasePath() 方法
public string getBasePath()
{return} string 模块的根目录。Defaults to the directory containing the module class.
源码: framework/base/CModule.php#145 (显示)
public function getBasePath()
{
    if(
$this->_basePath===null)
    {
        
$class=new ReflectionClass(get_class($this));
        
$this->_basePath=dirname($class->getFileName());
    }
    return 
$this->_basePath;
}

返回模块的根目录。

getComponent() 方法
public IApplicationComponent getComponent(string $id, boolean $createIfNull=true)
$id string application component ID (case-sensitive)
$createIfNull boolean whether to create the component if it doesn't exist yet.
{return} IApplicationComponent the application component instance, null if the application component is disabled or does not exist.
源码: framework/base/CModule.php#382 (显示)
public function getComponent($id,$createIfNull=true)
{
    if(isset(
$this->_components[$id]))
        return 
$this->_components[$id];
    elseif(isset(
$this->_componentConfig[$id]) && $createIfNull)
    {
        
$config=$this->_componentConfig[$id];
        if(!isset(
$config['enabled']) || $config['enabled'])
        {
            
Yii::trace("Loading \"$id\" application component",'system.CModule');
            unset(
$config['enabled']);
            
$component=Yii::createComponent($config);
            
$component->init();
            return 
$this->_components[$id]=$component;
        }
    }
}

Retrieves the named application component.

参见

getComponents() 方法
public array getComponents(boolean $loadedOnly=true)
$loadedOnly boolean whether to return the loaded components only. If this is set false, then all components specified in the configuration will be returned, whether they are loaded or not. Loaded components will be returned as objects, while unloaded components as configuration arrays. This parameter has been available since version 1.1.3.
{return} array the application components (indexed by their IDs)
源码: framework/base/CModule.php#466 (显示)
public function getComponents($loadedOnly=true)
{
    if(
$loadedOnly)
        return 
$this->_components;
    else
        return 
array_merge($this->_componentConfig$this->_components);
}

返回应用程序组件。

getId() 方法
public string getId()
{return} string 模块的 ID。
源码: framework/base/CModule.php#127 (显示)
public function getId()
{
    return 
$this->_id;
}

返回模块的 ID。

getModule() 方法
public CModule getModule(string $id)
$id string 应用程序模块的 ID (case-sensitive)
{return} CModule the module instance, null if the module is disabled or does not exist.
源码: framework/base/CModule.php#269 (显示)
public function getModule($id)
{
    if(isset(
$this->_modules[$id]) || array_key_exists($id,$this->_modules))
        return 
$this->_modules[$id];
    elseif(isset(
$this->_moduleConfig[$id]))
    {
        
$config=$this->_moduleConfig[$id];
        if(!isset(
$config['enabled']) || $config['enabled'])
        {
            
Yii::trace("Loading \"$id\" module",'system.base.CModule');
            
$class=$config['class'];
            unset(
$config['class'], $config['enabled']);
            if(
$this===Yii::app())
                
$module=Yii::createComponent($class,$id,null,$config);
            else
                
$module=Yii::createComponent($class,$this->getId().'/'.$id,$this,$config);
            return 
$this->_modules[$id]=$module;
        }
    }
}

检索指定的应用程序模块。 The module has to be declared in modules. A new instance will be created when calling this method with the given ID for the first time.

getModulePath() 方法
public string getModulePath()
{return} string the directory that contains the application modules. Defaults to the 'modules' subdirectory of basePath.
源码: framework/base/CModule.php#199 (显示)
public function getModulePath()
{
    if(
$this->_modulePath!==null)
        return 
$this->_modulePath;
    else
        return 
$this->_modulePath=$this->getBasePath().DIRECTORY_SEPARATOR.'modules';
}

Returns the directory that contains the application modules.

getModules() 方法
public array getModules()
{return} array the configuration of the currently installed modules (module ID => configuration)
源码: framework/base/CModule.php#305 (显示)
public function getModules()
{
    return 
$this->_moduleConfig;
}

Returns the configuration of the currently installed modules.

getParams() 方法
public CAttributeCollection getParams()
{return} CAttributeCollection 用户定义的参数列表
源码: framework/base/CModule.php#172 (显示)
public function getParams()
{
    if(
$this->_params!==null)
        return 
$this->_params;
    else
    {
        
$this->_params=new CAttributeCollection;
        
$this->_params->caseSensitive=true;
        return 
$this->_params;
    }
}

返回用户定义的参数。

getParentModule() 方法
public CModule getParentModule()
{return} CModule 父模块。Null if this module does not have a parent.
源码: framework/base/CModule.php#257 (显示)
public function getParentModule()
{
    return 
$this->_parentModule;
}

返回父模块。

hasComponent() 方法
public boolean hasComponent(string $id)
$id string application component ID
{return} boolean whether the named application component exists (including both loaded and disabled.)
源码: framework/base/CModule.php#370 (显示)
public function hasComponent($id)
{
    return isset(
$this->_components[$id]) || isset($this->_componentConfig[$id]);
}

Checks whether the named component exists.

hasModule() 方法 (自版本 v1.1.2 可用)
public boolean hasModule(string $id)
$id string 模块 ID
{return} boolean whether the specified module is installed.
源码: framework/base/CModule.php#296 (显示)
public function hasModule($id)
{
    return isset(
$this->_moduleConfig[$id]) || isset($this->_modules[$id]);
}

Returns a value indicating whether the specified module is installed.

init() 方法
protected void init()
源码: framework/base/CModule.php#551 (显示)
protected function init()
{
}

初始化模块。 This method is called at the end of the module constructor. Note that at this moment, the module has been configured, the behaviors have been attached and the application components have been registered.

参见

preinit() 方法
protected void preinit()
源码: framework/base/CModule.php#540 (显示)
protected function preinit()
{
}

Preinitializes the module. This method is called at the beginning of the module constructor. You may override this method to do some customized preinitialization work. Note that at this moment, the module is not configured yet.

参见

preloadComponents() 方法
protected void preloadComponents()
源码: framework/base/CModule.php#527 (显示)
protected function preloadComponents()
{
    foreach(
$this->preload as $id)
        
$this->getComponent($id);
}

加载静态应用程序组件。

setAliases() 方法
public void setAliases(array $mappings)
$mappings array list of aliases to be defined. The array keys are root aliases, while the array values are paths or aliases corresponding to the root aliases. 例如,
array(
   'models'=>'application.models',              // an existing alias
   'extensions'=>'application.extensions',      // an existing alias
   'backend'=>dirname(__FILE__).'/../backend',  // a directory
)
源码: framework/base/CModule.php#242 (显示)
public function setAliases($mappings)
{
    foreach(
$mappings as $name=>$alias)
    {
        if((
$path=Yii::getPathOfAlias($alias))!==false)
            
Yii::setPathOfAlias($name,$path);
        else
            
Yii::setPathOfAlias($name,$alias);
    }
}

Defines the root aliases.

setBasePath() 方法
public void setBasePath(string $path)
$path string 模块的根目录。
源码: framework/base/CModule.php#161 (显示)
public function setBasePath($path)
{
    if((
$this->_basePath=realpath($path))===false || !is_dir($this->_basePath))
        throw new 
CException(Yii::t('yii','Base path "{path}" is not a valid directory.',
            array(
'{path}'=>$path)));
}

设置模块的根目录。 This method can only be invoked at the beginning of the constructor.

setComponent() 方法
public void setComponent(string $id, array|IApplicationComponent $component, boolean $merge=true)
$id string 组件 ID
$component array|IApplicationComponent application component (either configuration array or instance). If this parameter is null, component will be unloaded from the module.
$merge boolean whether to merge the new component configuration with the existing one. Defaults to true, meaning the previously registered component configuration with the same ID will be merged with the new configuration. If set to false, the existing configuration will be replaced completely. This parameter is available since 1.1.13.
源码: framework/base/CModule.php#414 (显示)
public function setComponent($id,$component,$merge=true)
{
    if(
$component===null)
    {
        unset(
$this->_components[$id]);
        return;
    }
    elseif(
$component instanceof IApplicationComponent)
    {
        
$this->_components[$id]=$component;

        if(!
$component->getIsInitialized())
            
$component->init();

        return;
    }
    elseif(isset(
$this->_components[$id]))
    {
        if(isset(
$component['class']) && get_class($this->_components[$id])!==$component['class'])
        {
            unset(
$this->_components[$id]);
            
$this->_componentConfig[$id]=$component//we should ignore merge here
            
return;
        }

        foreach(
$component as $key=>$value)
        {
            if(
$key!=='class')
                
$this->_components[$id]->$key=$value;
        }
    }
    elseif(isset(
$this->_componentConfig[$id]['class'],$component['class'])
        && 
$this->_componentConfig[$id]['class']!==$component['class'])
    {
        
$this->_componentConfig[$id]=$component//we should ignore merge here
        
return;
    }

    if(isset(
$this->_componentConfig[$id]) && $merge)
        
$this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$component);
    else
        
$this->_componentConfig[$id]=$component;
}

Puts a component under the management of the module. The component will be initialized by calling its init() method if it has not done so.

setComponents() 方法
public void setComponents(array $components, boolean $merge=true)
$components array application components(id=>component configuration or instances)
$merge boolean whether to merge the new component configuration with the existing one. Defaults to true, meaning the previously registered component configuration of the same ID will be merged with the new configuration. If false, the existing configuration will be replaced completely.
源码: framework/base/CModule.php#505 (显示)
public function setComponents($components,$merge=true)
{
    foreach(
$components as $id=>$component)
        
$this->setComponent($id,$component,$merge);
}

设置应用程序组件。

When a configuration is used to specify a component, it should consist of the component's initial property values (name-value pairs). Additionally, a component can be enabled (default) or disabled by specifying the 'enabled' value in the configuration.

If a configuration is specified with an ID that is the same as an existing component or configuration, the existing one will be replaced silently.

The following is the configuration for two components:

array(
    'db'=>array(
        'class'=>'CDbConnection',
        'connectionString'=>'sqlite:path/to/file.db',
    ),
    'cache'=>array(
        'class'=>'CDbCache',
        'connectionID'=>'db',
        'enabled'=>!YII_DEBUG,  // enable caching in non-debug mode
    ),
)

setId() 方法
public void setId(string $id)
$id string 模块 ID
源码: framework/base/CModule.php#136 (显示)
public function setId($id)
{
    
$this->_id=$id;
}

设置模块的 ID。

setImport() 方法
public void setImport(array $aliases)
$aliases array list of aliases to be imported
源码: framework/base/CModule.php#223 (显示)
public function setImport($aliases)
{
    foreach(
$aliases as $alias)
        
Yii::import($alias);
}

设置用于在模块中使用的别名。

setModulePath() 方法
public void setModulePath(string $value)
$value string the directory that contains the application modules.
源码: framework/base/CModule.php#212 (显示)
public function setModulePath($value)
{
    if((
$this->_modulePath=realpath($value))===false || !is_dir($this->_modulePath))
        throw new 
CException(Yii::t('yii','The module path "{path}" is not a valid directory.',
            array(
'{path}'=>$value)));
}

Sets the directory that contains the application modules.

setModules() 方法
public void setModules(array $modules, boolean $merge=true)
$modules array module configurations.
$merge boolean whether to merge the new module configuration with the existing one. Defaults to true, meaning the previously registered module configuration with the same ID will be merged with the new configuration. If set to false, the existing configuration will be replaced completely. This parameter is available since 1.1.16.
源码: framework/base/CModule.php#341 (显示)
public function setModules($modules,$merge=true)
{
    foreach(
$modules as $id=>$module)
    {
        if(
is_int($id))
        {
            
$id=$module;
            
$module=array();
        }
        if(isset(
$this->_moduleConfig[$id]) && $merge)
            
$this->_moduleConfig[$id]=CMap::mergeArray($this->_moduleConfig[$id],$module);
        else
        {
            if(!isset(
$module['class']))
            {
                if (
Yii::getPathOfAlias($id)===false)
                    
Yii::setPathOfAlias($id,$this->getModulePath().DIRECTORY_SEPARATOR.$id);
                
$module['class']=$id.'.'.ucfirst($id).'Module';
            }
            
$this->_moduleConfig[$id]=$module;
        }
    }
}

Configures the sub-modules of this module.

Call this method to declare sub-modules and configure them with their initial property values. The parameter should be an array of module configurations. Each array element represents a single module, which can be either a string representing the module ID or an ID-configuration pair representing a module with the specified ID and the initial property values.

For example, the following array declares two modules:

array(
    'admin',                // a single module ID
    'payment'=>array(       // ID-configuration pair
        'server'=>'paymentserver.com',
    ),
)


By default, the module class is determined using the expression ucfirst($moduleID).'Module'. And the class file is located under modules/$moduleID. You may override this default by explicitly specifying the 'class' option in the configuration.

You may also enable or disable a module by specifying the 'enabled' option in the configuration.

setParams() 方法
public void setParams(array $value)
$value array 用户定义的参数。应该设置为 name-value 对。
源码: framework/base/CModule.php#188 (显示)
public function setParams($value)
{
    
$params=$this->getParams();
    foreach(
$value as $k=>$v)
        
$params->add($k,$v);
}

设置用户定义的参数