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,也即是我们自定义的组件,
- <?php
- namespace app\component;
- use Yii;
- use yii\base\Component;
- use yii\base\InvalidConfigException;
- class MyComponent extends Component
- {
- public $terry;
- public functionwelcome()
- {
- 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 functionwelcome1()
- {
- $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();
- echo1;exit;
可以看到输出:
xxxxHello..Welcome to MyComponent tttttttt1
4.在其他模块调用Report模块的组件:
- $module = Yii::$app->getModule("report");
- $module->mycomponent->welcome();
- //Yii::$app->mycomponent->welcome();
- echo1;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 functionconfigModuleParams(){
- # 配置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的原理,以及细致的详解与剖析
文章来源
- yii2 添加 自定义 组件 custom component,以及模块 module 原理的详解剖析
- yii2 扩展和自定义(函数 , 组件 , 模块)
- 插件: images and custom options,magento添加多图片,自定义选项custom options,catelog,分类的插件以及使用方法!!!
- Custom Components(自定义组件)
- Custom Component, Unreal Engine里定制组件
- Yii2 自定义组件
- yii2自定义组件
- yii2.0自定义组件
- YII2.0自定义组件
- 物体的刚体组件Rigidbody component 详解
- Glide 4.x添加自定义组件原理
- YII2 学习笔记 --- 创建模块 Module
- yii2添加module配置和常见问题解决
- Android自定义组件添加组件的属性
- GNU Radio自定义模块在添加到流图后运行报‘module’ object has no attribute错误的解决方法
- module 与 component 的区别
- yii2的分页组件
- vue component为什么官网的自定义组件不显示及各种自定义组件写法?
- 程序员面试100题之四:求1+2+...+n
- CI PHP7 session 不能读取的问题
- SSM框架——使用MyBatis Generator自动创建代码
- html5+go+websocket简单实例代码
- File I/O
- yii2 添加 自定义 组件 custom component,以及模块 module 原理的详解剖析
- Android应用开发编译框架流程与IDE及Gradle概要
- 《C++ primer》5th读书笔记(chapter3)
- 解决tableViewCell分割线不到左边界的问题
- C++ error: unknown type name
- 一.makefile 基础认识
- 用结构化思维策划一个会议
- jQuery解除事件绑定 unbind
- jeesite 记住用户名密码