Yii框架学习笔记之应用组件操作示例
作者:倾听岁月 发布时间:2024-05-11 09:23:07
标签:Yii,应用组件
本文实例讲述了Yii框架学习笔记之应用组件操作。分享给大家供大家参考,具体如下:
所有的组件都应声明在config/web.php
//组件声明在该数组下
'components'=>array(
//自定义组件1 - 函数形式
'customComponent1' => function(){
$custom = new app\components\CustomComponent\realization\CustomComponent1();
$custom->setName('谭勇');
$custom->setAge(22);
return $custom;
},
//自定义组件2 - 数组形式
'customComponent2' => array(
'class' => 'app\components\CustomComponent\relazation\CustomComponent2'
'name' => '谭勇',
'age' => 22
),
//自定义组件 - 字符串形式
'customComponent3' => 'app\components\CustomComponent\realization\CustomComponent3'
),
如果只是在components 中声明了该组件,那么只有在首次调用的时候才会实例化这个组件,之后调用都会复用之前的实例。 如果你在bootstrap 数组中声明了这个组件,那么该组件会随着应用主体的创建而实例(也就是默认会被实例,而不是首次调用才会实例这个组件)。
//默认加载customComponent1 和 customComponent2 组件
'bootstrap' => array(
'customComponent1','customComponent2'
),
在应用目录下创建 components 目录
组件 CutomComponent
接口类 app\components\CustomComponent\CustomComponent;
<?php
namespace app\components\CustomComponent;
interface CustomComponent
{
public function setName($name);
public function setAge($age);
public function getName();
public function getAge();
}
?>
接口实现类 app\components\CustomComponent\realization\CustomComponent1
<?php
namespace app\components\CustomComponent\realization;
use app\components\CustomComponent\CustomComponent;
class CustomComponent1 implments CustomComponent
{
public $name='勇哥';
public $age = '我的年龄';
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setAge($age)
{
$this->age = $age;
}
public function getAge()
{
return $this->age;
}
}
?>
customComponent2,customComponent3 我们都让他们与customComponent1 具有相同的代码。 那么我们怎么去调用这些组件呢?
namespace app\controllers\home;
use Yii;
use yii\web\Controller;
class IndexController extends Controller
{
public function actionIndex()
{
//组件customComponent1
echo Yii::$app->customComponent1->getName();
//组件customComponent2
echo Yii::$app->customComponent2->getName();
//组件customComponent3
echo Yii::$app->customComponent3->getName();
}
}
然后回过头看数组形式、函数形式、字符串形式的组件
//函数形式 - 这个很容易理解 实例化后设置属性值
function(){
$custom = new app\components\CustomComponent\realization\CustomComponent1();
$custom->setName('谭勇');
$custom->setAge(22);
return $custom;
},
//数组形式 - 它会实例化这个组件 之后设置属性值 注意这里设置属性值的方法 和 函数不一样,它是 $custom->name = '谭勇' , $custom->age = 22
array(
'class' => 'app\components\CustomComponent\relazation\CustomComponent2'
'name' => '谭勇',
'age' => 22
),
//字符串形式 只知道会实例化这个组件,怎么注入属性值,这个不清楚支不支持
组件有什么作用?
如果你理解Java spring mvc 那么就不难理解组件的作用 可以作为服务层,数据访问层等等
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
来源:https://blog.csdn.net/u014559227/article/details/77498946


猜你喜欢
- 一、背景有一段代码,因为调用了封装的方法,所以会有一些本来不需要的返回值。这些返回值该怎么处理才能让它消失呢。有的人会说,清除变量不就好了吗
- 在Python语言中最常见的括号有三种,分别是:小括号()、中括号[]、花括号{};其作用也不相同,分别用来代表不同的Python基本内置数
- 本文实例讲述了go语言日志记录库简单使用方法。分享给大家供大家参考。具体实现方法如下:package mainimport ( &
- 今天在看框架的时候无意间看到了document.compatMode,经过一番资料查找,终于搞懂了。文档模式在开发中貌似很少用到,最常见的是
- 实现一个mysql数据库封装需要考虑的问题1.使用方便性采用直接sql语句操作方式。只要会写sql语句,那么将没有其他学习成本。uctphp
- CASE 表达式分为简单表达式与搜索表达式,其中搜索表达式可以覆盖简单表达式的全部能力,我也建议只写搜索表达式,而不要写简单表达式。简单表达
- 1、异常出现的场景.:在使用Hibernate做为项目持久层的情况下,需要对某一张表进行一个扩展,扩展操作便是在该表上创建一个触发器。将表中
- 题目:来自Madrid且订单数少于3的消费者 建表:set nocount on --当 SET NOCOUNT 为
- 整数的阶乘(英语:factorial)是所有小于及等于该数的正整数的积,0的阶乘为1。即:n!=1×2×3×...×n。首先导入math模块
- 一、QtDesigner介绍Qt Designer 是一款GUI界面工具,可以实现将UI设计界面转为Python代码的工具;二、安装 QTd
- 什么是面向对象编程(类)利用(面向)对象的(属性和方法)去进行编码的过程即面向对象编程自定义对象数据类型就是面向对象中的类(class)的概
- **一 tf.concat( ) 函数–合并**In [2]: a = tf.ones([4,35,8]) &n
- 我的需求:手动配置X轴、Y轴、图表标题等参数自动通过Pyecharts模块生成可视化的html数据图表,并将浏览器图表展示到UI界面上。制作
- 问题描述分析这是因为本地delve组件版本过低导致的,2019.2.1版本的Goland默认支持go 1.13查看F:\Go (GOPATH
- 本文实例讲述了Golang编程实现删除字符串中出现次数最少字符的方法。分享给大家供大家参考,具体如下:描述:实现删除字符串中出现次数最少的字
- 最近对微格式进行了一些学习,在学习过程中收获不少。在此分享下,欢迎交流!微型格式的优点:1,语义化的HTML和CSS类名称来标记共同内容。2
- 1. 复制表结构及其数据:create table table_name_new as select * from table_name_o
- 前言URLconf 就像是 Django 所支撑网站的目录。它的本质是 URL 模式以及要为该 URL 模式调用的视图函数之间的映射表。你就
- 下载驱动器http://chromedriver.storage.googleapis.com/index.html下载与谷歌版本相同或最近
- 在Python中如何实现单例模式?这可以说是一个经典的Python面试题了。这回我们讲讲实现Python中实现单例模式的n种方式,和它的原理