Yii Framework v1.1.10 绫诲弬鑰

CModule

system.base
缁ф壙 abstract class CModule » CComponent
瀛愮被 CApplication, CWebModule
鐗堟湰 $Id: CModule.php 3515 2011-12-28 12:29:24Z mdomba $
婧愮爜 framework/base/CModule.php
CModule鏄ā鍧楀拰搴旂敤绋嬪簭绫荤殑鍩虹被銆

CModule涓昏绠$悊搴旂敤缁勪欢鍜屽瓙妯″潡銆

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
basePath string 杩斿洖妯″潡鏍圭洰褰曘 CModule
behaviors array the behaviors that should be attached to the module. CModule
components array 杩斿洖搴旂敤缁勪欢銆 CModule
id string 杩斿洖妯″潡ID銆 CModule
modulePath string 杩斿洖鍖呭惈搴旂敤绋嬪簭妯″潡鐨勭洰褰曘 CModule
modules array 杩斿洖搴旂敤绋嬪簭褰撳墠宸茬粡瀹夎鐨勬ā鍧椼 CModule
params CAttributeCollection 杩斿洖鐢ㄦ埛瀹氫箟鐨勫弬鏁般 CModule
parentModule CModule 杩斿洖鐖舵ā鍧椼 CModule
preload array the IDs of the application components that should be preloaded. CModule

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__construct() 鏋勯犳柟娉曘 CModule
__get() getter榄旀湳鏂规硶銆 CModule
__isset() 妫鏌ヤ竴涓睘鎬у兼槸鍚︿负null銆 CModule
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
configure() 涓烘ā鍧楁寚瀹氶厤缃 CModule
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
getBasePath() 杩斿洖妯″潡鏍圭洰褰曘 CModule
getComponent() Retrieves the named application component. CModule
getComponents() 杩斿洖搴旂敤缁勪欢銆 CModule
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getId() 杩斿洖妯″潡ID銆 CModule
getModule() 妫绱㈡寚瀹氱殑搴旂敤妯″潡銆 CModule
getModulePath() 杩斿洖鍖呭惈搴旂敤绋嬪簭妯″潡鐨勭洰褰曘 CModule
getModules() 杩斿洖搴旂敤绋嬪簭褰撳墠宸茬粡瀹夎鐨勬ā鍧椼 CModule
getParams() 杩斿洖鐢ㄦ埛瀹氫箟鐨勫弬鏁般 CModule
getParentModule() 杩斿洖鐖舵ā鍧椼 CModule
hasComponent() 妫鏌ユ槸鍚﹀瓨鍦ㄦ寚瀹氱粍浠躲 CModule
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasModule() 妫鏌ユ槸鍚﹀凡缁忓畨瑁呬簡鎸囧畾妯″潡銆 CModule
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
setAliases() 瀹氫箟鏍圭洰褰曠殑鍒悕銆 CModule
setBasePath() 璁剧疆鍧楁牴鐩綍銆 CModule
setComponent() 娣诲姞涓涓粍浠跺埌妯″潡涓 CModule
setComponents() 璁剧疆搴旂敤缁勪欢銆 CModule
setId() 璁剧疆妯″潡ID銆 CModule
setImport() 璁剧疆鍦ㄦā鍧椾腑浣跨敤鐨勮矾寰勫埆鍚嶃 CModule
setModulePath() 璁剧疆鍖呭惈搴旂敤绋嬪簭妯″潡鐨勭洰褰曘 CModule
setModules() 閰嶇疆妯″潡鐨勫瓙妯″潡銆 CModule
setParams() 璁剧疆鐢ㄦ埛瀹氫箟鐨勫弬鏁般 CModule

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
init() 鍒濆鍖栨ā鍧椼 CModule
preinit() 妯″潡棰勫垵濮嬪寲銆 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)

杩斿洖鍖呭惈搴旂敤绋嬪簭妯″潡鐨勭洰褰曘

modules 灞炴
public array getModules()
public void setModules(array $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 the ID of this module
$parent CModule the parent module (if any)
$config mixed the module configuration. It can be either an array or the path of a PHP file returning the configuration array.
婧愮爜锛 framework/base/CModule.php#70 (鏄剧ず)
public function __construct($id,$parent,$config=null)
{
    
$this->_id=$id;
    
$this->_parentModule=$parent;

    
// set basePath at 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 application component or property name
{return} mixed the named property value
婧愮爜锛 framework/base/CModule.php#101 (鏄剧ず)
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 the property name or the event name
{return} boolean whether the property value is null
婧愮爜锛 framework/base/CModule.php#116 (鏄剧ず)
public function __isset($name)
{
    if(
$this->hasComponent($name))
        return 
$this->getComponent($name)!==null;
    else
        return 
parent::__isset($name);
}

妫鏌ヤ竴涓睘鎬у兼槸鍚︿负null銆 This method overrides the parent implementation by checking if the named application component is loaded.

configure() 鏂规硶
public void configure(array $config)
$config array the configuration array
婧愮爜锛 framework/base/CModule.php#478 (鏄剧ず)
public function configure($config)
{
    if(
is_array($config))
    {
        foreach(
$config as $key=>$value)
            
$this->$key=$value;
    }
}

涓烘ā鍧楁寚瀹氶厤缃

getBasePath() 鏂规硶
public string getBasePath()
{return} string the root directory of the module. Defaults to the directory containing the module class.
婧愮爜锛 framework/base/CModule.php#146 (鏄剧ず)
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#376 (鏄剧ず)
public function getComponent($id,$createIfNull=true)
{
    if(isset(
$this->_components[$id]))
        return 
$this->_components[$id];
    else if(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#422 (鏄剧ず)
public function getComponents($loadedOnly=true)
{
    if(
$loadedOnly)
        return 
$this->_components;
    else
        return 
array_merge($this->_componentConfig$this->_components);
}

杩斿洖搴旂敤缁勪欢銆

getId() 鏂规硶
public string getId()
{return} string the module ID.
婧愮爜锛 framework/base/CModule.php#128 (鏄剧ず)
public function getId()
{
    return 
$this->_id;
}

杩斿洖妯″潡ID銆

getModule() 鏂规硶
public CModule getModule(string $id)
$id string application module ID (case-sensitive)
{return} CModule the module instance, null if the module is disabled or does not exist.
婧愮爜锛 framework/base/CModule.php#270 (鏄剧ず)
public function getModule($id)
{
    if(isset(
$this->_modules[$id]) || array_key_exists($id,$this->_modules))
        return 
$this->_modules[$id];
    else if(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#200 (鏄剧ず)
public function getModulePath()
{
    if(
$this->_modulePath!==null)
        return 
$this->_modulePath;
    else
        return 
$this->_modulePath=$this->getBasePath().DIRECTORY_SEPARATOR.'modules';
}

杩斿洖鍖呭惈搴旂敤绋嬪簭妯″潡鐨勭洰褰曘

getModules() 鏂规硶
public array getModules()
{return} array the configuration of the currently installed modules (module ID => configuration)
婧愮爜锛 framework/base/CModule.php#306 (鏄剧ず)
public function getModules()
{
    return 
$this->_moduleConfig;
}

杩斿洖搴旂敤绋嬪簭褰撳墠宸茬粡瀹夎鐨勬ā鍧椼

getParams() 鏂规硶
public CAttributeCollection getParams()
{return} CAttributeCollection the list of user-defined parameters
婧愮爜锛 framework/base/CModule.php#173 (鏄剧ず)
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 the parent module. Null if this module does not have a parent.
婧愮爜锛 framework/base/CModule.php#258 (鏄剧ず)
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#364 (鏄剧ず)
public function hasComponent($id)
{
    return isset(
$this->_components[$id]) || isset($this->_componentConfig[$id]);
}

妫鏌ユ槸鍚﹀瓨鍦ㄦ寚瀹氱粍浠躲

hasModule() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.2锛
public boolean hasModule(string $id)
$id string the module ID
{return} boolean whether the specified module is installed.
婧愮爜锛 framework/base/CModule.php#297 (鏄剧ず)
public function hasModule($id)
{
    return isset(
$this->_moduleConfig[$id]) || isset($this->_modules[$id]);
}

妫鏌ユ槸鍚﹀凡缁忓畨瑁呬簡鎸囧畾妯″潡銆

init() 鏂规硶
protected void init()
婧愮爜锛 framework/base/CModule.php#514 (鏄剧ず)
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#503 (鏄剧ず)
protected function preinit()
{
}

妯″潡棰勫垵濮嬪寲銆 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#490 (鏄剧ず)
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. For example,
array(
   'models'=>'application.models',              // an existing alias
   'extensions'=>'application.extensions',      // an existing alias
   'backend'=>dirname(__FILE__).'/../backend',  // a directory
)
婧愮爜锛 framework/base/CModule.php#243 (鏄剧ず)
public function setAliases($mappings)
{
    foreach(
$mappings as $name=>$alias)
    {
        if((
$path=Yii::getPathOfAlias($alias))!==false)
            
Yii::setPathOfAlias($name,$path);
        else
            
Yii::setPathOfAlias($name,$alias);
    }
}

瀹氫箟鏍圭洰褰曠殑鍒悕銆

setBasePath() 鏂规硶
public void setBasePath(string $path)
$path string the root directory of the module.
婧愮爜锛 framework/base/CModule.php#162 (鏄剧ず)
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, IApplicationComponent $component)
$id string component ID
$component IApplicationComponent the component to be added to the module. If this parameter is null, it will unload the component from the module.
婧愮爜锛 framework/base/CModule.php#402 (鏄剧ず)
public function setComponent($id,$component)
{
    if(
$component===null)
        unset(
$this->_components[$id]);
    else
    {
        
$this->_components[$id]=$component;
        if(!
$component->getIsInitialized())
            
$component->init();
    }
}

娣诲姞涓涓粍浠跺埌妯″潡涓 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#461 (鏄剧ず)
public function setComponents($components,$merge=true)
{
    foreach(
$components as $id=>$component)
    {
        if(
$component instanceof IApplicationComponent)
            
$this->setComponent($id,$component);
        else if(isset(
$this->_componentConfig[$id]) && $merge)
            
$this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$component);
        else
            
$this->_componentConfig[$id]=$component;
    }
}

璁剧疆搴旂敤缁勪欢銆

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 the module ID
婧愮爜锛 framework/base/CModule.php#137 (鏄剧ず)
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#224 (鏄剧ず)
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#213 (鏄剧ず)
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)));
}

璁剧疆鍖呭惈搴旂敤绋嬪簭妯″潡鐨勭洰褰曘

setModules() 鏂规硶
public void setModules(array $modules)
$modules array module configurations.
婧愮爜锛 framework/base/CModule.php#337 (鏄剧ず)
public function setModules($modules)
{
    foreach(
$modules as $id=>$module)
    {
        if(
is_int($id))
        {
            
$id=$module;
            
$module=array();
        }
        if(!isset(
$module['class']))
        {
            
Yii::setPathOfAlias($id,$this->getModulePath().DIRECTORY_SEPARATOR.$id);
            
$module['class']=$id.'.'.ucfirst($id).'Module';
        }

        if(isset(
$this->_moduleConfig[$id]))
            
$this->_moduleConfig[$id]=CMap::mergeArray($this->_moduleConfig[$id],$module);
        else
            
$this->_moduleConfig[$id]=$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 user-defined parameters. This should be in name-value pairs.
婧愮爜锛 framework/base/CModule.php#189 (鏄剧ず)
public function setParams($value)
{
    
$params=$this->getParams();
    foreach(
$value as $k=>$v)
        
$params->add($k,$v);
}

璁剧疆鐢ㄦ埛瀹氫箟鐨勫弬鏁般

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