本文主要说的是两种情况:
1.在yii2中自定义全局组件:yii2 custom component
2. yii2模块中自定义组件的实现方式:yii2 module custom component
yii2中定义了很多的组件,我们可以自己添加一个组件,步骤如下:
1.建立文件MyComponent.php,也即是我们自定义的组件,
<?php
namespace app\component;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
 
class MyComponent extends Component
{
  
  public $terry;
  
  public function welcome()
  {
    echo $this->terry."Hello..Welcome to MyComponent";
  }
 
}
custom component ,自定义组件。
2.添加配置,在config.php文件的component中:
'mycomponent' => [
     'class' => 'appadmin\component\MyComponent',
     'terry' => 'xxxx',
 ],
3.调用:
Yii::$app->mycomponent->welcome();
可以看到以下的输出:xxxxHello..Welcome to MyComponent1
4.另外我们希望在模块里面配置自定义的组件
模块 module 原理的详解剖析:
这里需要说一下模块,对于没有模块的时候,我们的主体是application,这个类继承与\yii\base\Component, 在某种程序上module 就是一个微缩版的application,应用主体有的功能,譬如参数,组件等配置,module都可以使用,在使用前我们需要先得到module。得到module的方式有如下两种:
# 通过moduleName得到module $module = Yii::$app->getModule($moduleName) # 得到当前的module。 $module = Yii::$app->controller->module
得到了module,我们就可以像使用application那样使用,譬如应用主体下面使用组件:
$app = Yii::$app; $component = $app->mycomponent; 而模块里面使用为: $module = Yii::$app->controller->module $component = $module->mycomponent; #在模块中获取配置参数: $param = $module->params[$param]; #在上面可以看出,在模块中的方法的使用,和应用主体中使用很类似 $module 相当于 Yii::$app
在模块中配置参数:
您可以在模块的配置中添加,在params参数中添加,譬如如下:
'modules'=>[
    'report' => [
        'class' => 'appadmin\code\Report\Module',
    
        'components'=>[
          'mycomponent' => [
            'class' => 'appadmin\component\MyComponent',
            'terry' => 'xxxx',
          ],
        ],
    
        'params' => [
          'water' => 'good',
        ],
    ],
],
也可以在Module.php文件的init中添加params
Yii::configure($this, ['params'=> $params_data]);
下面我们写一下在模块里面配置组件的例子:
1.模块的定义:
'modules'=>[
    'report' => [
        'class' => 'appadmin\code\Report\Module',
    
        'components'=>[
          'mycomponent' => [
            'class' => 'appadmin\component\MyComponent',
            'terry' => 'xxxx',
          ],
        ],
    
        'params' => [
          'water' => 'good',
        ],
    ],
],
2.组件文件定义:
<?php
namespace appadmin\component;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
 
class MyComponent extends Component
{
  
  public $terry;
  
  public function welcome1()
  {
    $module_param_water = Yii::$app->controller->module->params['water'];
    echo $this->terry."Hello..Welcome to MyComponent ".$module_param_water;
  }
 
}
3.调用:
$module = Yii::$app->controller->module; $module->mycomponent->welcome(); //Yii::$app->mycomponent->welcome(); echo 1;exit;
可以看到输出:
xxxxHello..Welcome to MyComponent tttttttt1
4.在其他模块调用Report模块的组件:
$module = Yii::$app->getModule("report");
$module->mycomponent->welcome();
//Yii::$app->mycomponent->welcome();
echo 1;exit;
输出结果为:
xxxxHello..Welcome to MyComponent 1
可以看到结果与上面的不同。这是因为welcome1函数定义:
$module_param_water = Yii::$app->controller->module->params['water'];
module_param_water 是当前模块下面的配置,而在当前module下面没有配置这个模块,因此,这个值为空。
下面是模块的module文件的对params的配置方法:用来合并config.php里面param的配置,和在模块文件里面./etc/config.php文件里面的配置。
public function configModuleParams(){
    # 配置config文件
    $config_file_dir = $this->_currentDir . '/etc/config.php';
    if(file_exists($config_file_dir)){
      $params_data = (require($config_file_dir));
      
    }
    # 设置参数
    $params_data['_currentDir'] 		= $this->_currentDir;
    $params_data['_currentNameSpace'] 	= $this->_currentNameSpace;
    $params = $this->params;
    if(is_array($params) && !empty($params)){
      $params_data = \yii\helpers\ArrayHelper::merge($params,$params_data);
    }
    Yii::configure($this, ['params'=> $params_data]);
    
  }
到这里,我们可以看到yii2 module的原理,以及细致的详解与剖析