Yii框架中应用组件操作的示例分析

发布时间:2021-08-12 11:29:09 作者:小新
来源:亿速云 阅读:83

这篇文章主要介绍了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框架中应用组件操作的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. 微服务架构中SpringCloud基础组件应用设计的示例分析
  2. vue中组件参数的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

yii

上一篇:SpringCloud中怎么利用Hystrix组件实现服务熔断

下一篇:Python线上环境使用日志的及配置文件的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》