Yii框架的应用组件如何定义、创建及使用
Admin 2022-05-30 群英技术资讯 1022 次浏览
今天就跟大家聊聊有关“Yii框架的应用组件如何定义、创建及使用”的内容,可能很多人都不太了解,为了让大家认识和更进一步的了解,小编给大家总结了以下内容,希望这篇“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 那么就不难理解组件的作用 可以作为服务层,数据访问层等等
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
生成器提供了一种更容易实现简单对象迭代的方法。与定义类相比,性能支出和复杂性大大降低。生成器允许在foreach代码块中写代码来迭代一组数据,而不需要在内存中创建一个数组,这将使内存达到上限或占据相当大的处理时间。
我们在本篇文章里给大家整理了关于PHP中fclose函数的详细用法和知识点,需要的朋友们学习下。
php想调用swoole时必须要开启Swoole扩展。Swoole是C语言编写,采用编译安装的方式。Swoole是php的服务器异步方案之一。
如何优化页面响应时间:动态页面静态化优化数据库使用负载均衡使用缓存如果页面中的一些内容不经常改动,可以使用动态页面静态化。好处是:减少服务器脚本的计算时间;降低服务器的响应时间。1、动态URL地址设置静态形式(伪静态)例如:http://xxx.com/index.php?c=play&id=1111 =>http://xxx.com/play/1111.h
本栏目讲解一个高性能、简单、跨平台的PHP7代码加密扩展,有需要的朋友可以看看,一起学习讨论。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008