yii2 添加 自定义 组件 custom component,以及模块 module 原理的详解剖析

来源:互联网 发布:ewb仿真软件下载 编辑:程序博客网 时间:2024/06/05 02:13

本文主要说的是两种情况:

1.在yii2中自定义全局组件:yii2 custom component

2. yii2模块中自定义组件的实现方式:yii2  module  custom component

yii2中定义了很多的组件,我们可以自己添加一个组件,步骤如下:

1.建立文件MyComponent.php,也即是我们自定义的组件,

  1. <?php
  2. namespace app\component;
  3. use Yii;
  4. use yii\base\Component;
  5. use yii\base\InvalidConfigException;
  6. class MyComponent extends Component
  7. {
  8. public $terry;
  9. public functionwelcome()
  10. {
  11. echo $this->terry."Hello..Welcome to MyComponent";
  12. }
  13. }

custom component ,自定义组件。

2.添加配置,在config.php文件的component中:

  1. 'mycomponent' =>[
  2. 'class' => 'appadmin\component\MyComponent',
  3. 'terry' => 'xxxx',
  4. ],

3.调用:

  1. Yii::$app->mycomponent->welcome();

可以看到以下的输出:xxxxHello..Welcome to MyComponent1

4.另外我们希望在模块里面配置自定义的组件

模块 module 原理的详解剖析:

这里需要说一下模块,对于没有模块的时候,我们的主体是application,这个类继承与\yii\base\Component,  在某种程序上module 就是一个微缩版的application,应用主体有的功能,譬如参数,组件等配置,module都可以使用,在使用前我们需要先得到module。得到module的方式有如下两种:

  1. # 通过moduleName得到module
  2. $module = Yii::$app->getModule($moduleName)
  3. # 得到当前的module。
  4. $module = Yii::$app->controller->module

得到了module,我们就可以像使用application那样使用,譬如应用主体下面使用组件:

  1. $app = Yii::$app;
  2. $component = $app->mycomponent;
  3. 而模块里面使用为:
  4. $module = Yii::$app->controller->module
  5. $component = $module->mycomponent;
  6. #在模块中获取配置参数:
  7. $param = $module->params[$param];
  8. #在上面可以看出,在模块中的方法的使用,和应用主体中使用很类似
  9. $module 相当于 Yii::$app

在模块中配置参数:

您可以在模块的配置中添加,在params参数中添加,譬如如下:

  1. 'modules'=>[
  2. 'report' => [
  3. 'class' => 'appadmin\code\Report\Module',
  4. 'components'=>[
  5. 'mycomponent' => [
  6. 'class' => 'appadmin\component\MyComponent',
  7. 'terry' => 'xxxx',
  8. ],
  9. ],
  10. 'params' => [
  11. 'water' => 'good',
  12. ],
  13. ],
  14. ],

 

也可以在Module.php文件的init中添加params

  1. Yii::configure($this,['params'=> $params_data]);

 

下面我们写一下在模块里面配置组件的例子:

1.模块的定义:

  1. 'modules'=>[
  2. 'report' => [
  3. 'class' => 'appadmin\code\Report\Module',
  4. 'components'=>[
  5. 'mycomponent' => [
  6. 'class' => 'appadmin\component\MyComponent',
  7. 'terry' => 'xxxx',
  8. ],
  9. ],
  10. 'params' => [
  11. 'water' => 'good',
  12. ],
  13. ],
  14. ],

2.组件文件定义:

  1. <?php
  2. namespace appadmin\component;
  3. use Yii;
  4. use yii\base\Component;
  5. use yii\base\InvalidConfigException;
  6. class MyComponent extends Component
  7. {
  8. public $terry;
  9. public functionwelcome1()
  10. {
  11. $module_param_water = Yii::$app->controller->module->params['water'];
  12. echo $this->terry."Hello..Welcome to MyComponent ".$module_param_water;
  13. }
  14. }

3.调用:

  1. $module = Yii::$app->controller->module;
  2. $module->mycomponent->welcome();
  3. //Yii::$app->mycomponent->welcome();
  4. echo1;exit;

可以看到输出:

xxxxHello..Welcome to MyComponent tttttttt1

4.在其他模块调用Report模块的组件:

  1. $module = Yii::$app->getModule("report");
  2. $module->mycomponent->welcome();
  3. //Yii::$app->mycomponent->welcome();
  4. echo1;exit;

输出结果为:

xxxxHello..Welcome to MyComponent 1

可以看到结果与上面的不同。这是因为welcome1函数定义:

  1. $module_param_water = Yii::$app->controller->module->params['water'];

module_param_water 是当前模块下面的配置,而在当前module下面没有配置这个模块,因此,这个值为空。

 

下面是模块的module文件的对params的配置方法:用来合并config.php里面param的配置,和在模块文件里面./etc/config.php文件里面的配置。

  1. public functionconfigModuleParams(){
  2. # 配置config文件
  3. $config_file_dir = $this->_currentDir .'/etc/config.php';
  4. if(file_exists($config_file_dir)){
  5. $params_data =(require($config_file_dir));
  6. }
  7. # 设置参数
  8. $params_data['_currentDir'] = $this->_currentDir;
  9. $params_data['_currentNameSpace'] = $this->_currentNameSpace;
  10. $params = $this->params;
  11. if(is_array($params) && !empty($params)){
  12. $params_data = \yii\helpers\ArrayHelper::merge($params,$params_data);
  13. }
  14. Yii::configure($this,['params'=> $params_data]);
  15. }

到这里,我们可以看到yii2 module的原理,以及细致的详解与剖析

 文章来源

0 0
原创粉丝点击