1,main.php 里面导入

'import' => array(
            'application.components.*'
        ),

2,application/components/BaseComponent.php

<?php
/**
 * 基础组件
 */
class BaseComponent extends CComponent
{
    public function init()
    {
    }
}

3,application/components/TestComponent.php

<?php
class TestComponent extends BaseComponent {
    public $title = 'aaxx';
    private $_url = 'ccdd';
//这里的get方法和set方法写了之后就可以直接调用私有属性!如果不允许直接调用则去掉这两个方法
    public function setUrl($value='') {
        $this->_url = $value;
    }
    public function getUrl() {
        return $this->_url ;
        }
        
    public function test()
    {
        return 'test';
    }

}

4,application/components/Controller.php

class Controller extends CController
{

public function init()
    {
        header('Content-type:text/html;charset=utf-8');

  }

/**
     * __get()
     * @see CComponent::__get()
     */
    public function __get($name)
    {
         if(substr(strtolower($name), -4) == 'comp') {
             return $this->getComponent($name);
         } else if(isset($this->default_tpl_vars[$name])) {
             return $this->default_tpl_vars[$name];
         }
         return parent::__get($name);
    }
    /**
     * @desc 获取组件
     * @author lijianwei
     */
    public function getComponent($name)
    {
       $name = strtolower($name);
       $component_id = substr($name, 0, -4);
       $component_class_name = ucfirst($component_id). 'Component';
       
       if(Yii::app()->hasComponent($component_id)) {
           return Yii::app()->getComponent($component_id);
       } else {
           Yii::app()->setComponent($component_id, array('class' => $component_class_name));
           return Yii::app()->getComponent($component_id);
       }
    }

}

5,控制器调用application/Controllers/HomeController.php

class HomeController extends Controller
{

public function actionIndex()
    {

    $b = $this->testComp->url='ddd';//因为有set方法,给组件私有变量赋值不报错
        echo $b;
        echo $this->testComp->test();

  }

}

yii 自定义组件的调用的更多相关文章

  1. Android开发之自定义组件和接口回调

    说到自定义控件不得不提的就是接口回调,在Android开发中接口回调用的还是蛮多的.在这篇博客开始的时候呢,我想聊一下iOS的自定义控件.在iOS中自定义控件的思路是继承自UIView, 在UIVie ...

  2. 微信小程序自定义组件,提示组件

    微信小程序自定义组件,这里列举了一个常用的提示自定义组件,调用自定义组件中的方法和字段.仅供参考和学习. 编写组件: 在根目录下添加“components”目录,然后像添加Page页面一样添加自定义组 ...

  3. yii中的自定义组件

    yii中的自定义组件(组件就是一些自定义的公用类) 1.在项目目录中的protected/components/Xxxx.php 2.在Xxxx.php中定义一个类,类名必须与文件名相同 3.控制器中 ...

  4. Flex自定义组件、皮肤,并调用

    标签:Flex  自定义组件  自定义皮肤  主应用调用模块 本程序样例学习自flex 实战教程.但因原教程代码不全,且根据个人需求有更改. 1文件列表 自定义as类Reveal.as,该类实现组件的 ...

  5. 微信小程序自定义组件的使用以及调用自定义组件中的方法

    在写小程序的时候,有时候页面的内容过多,逻辑比较复杂,如果全部都写在一个页面的话,会比较繁杂,代码可读性比较差,也不易于后期代码维护,这时候可以把里面某部分功能抽出来,单独封装为一个组件,也就是通常说 ...

  6. 微信小程序页面调用自定义组件内的事件

    微信小程序页面调用自定义组件内的事件 page page.json { "usingComponents": { "my-component": ". ...

  7. (转)Yii的组件机制之一:组件基础类CComponent分析

    Yii的组件机制 组件机制,是Yii整个体系的思想精髓,在使用Yii之前,最应该先了解其组件机制,如果不了解这个机制,那么阅读Yii源代码会非常吃力.组件机制给Yii框架赋予了无穷的灵活性和可扩展性, ...

  8. Yii2.0 高级模版编写使用自定义组件(component)

    翻译自:http://www.yiiframework.com/wiki/760/yii-2-0-write-use-a-custom-component-in-yii2-0-advanced-tem ...

  9. PhoneGap: Android 自定义组件

    Hello Core Demo Plugin Development(组件部署): http://docs.phonegap.com/en/2.0.0/guide_plugin-development ...

随机推荐

  1. Swift---TextView用法

    1.TextView放在一个NSScrollView中,作为documentView存在. @IBOutlet weak var txtScrollView: NSScrollView! 2.在Tex ...

  2. java中堆栈(stack)和堆(heap)(还在问静态变量放哪里,局部变量放哪里,静态区在哪里.....进来)

    (1)内存分配的策略 按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈式的,和堆式的. 静态存储分配是指在编译时就能确定每个数据目标在运行时刻的存储空间需求,因而在编 译时就可以给 ...

  3. CSS伪选择器的使用-遁地龙卷风

    分为伪元素选择器和伪类选择器两种,前者两个冒号,后者一个冒号,但是浏览器都看做一个冒号 1.a.::first-line 逐层匹配,直到有文本元素且结束改行为止 设置css属性word-break:b ...

  4. Promise 原理探究及其简单实现

    可移步 http://donglegend.com/2016/09/11/promise%E5%8E%9F%E7%90%86%E6%8E%A2%E7%A9%B6/ 观看 Promise是个什么玩意,大 ...

  5. jQuery对话框插件 ThickBox

    http://baike.haosou.com/doc/7607201-7881296.html 项目已经停止维护,但该插件还是不错的! ThickBox是一个基于JQuery类库的扩展,它能在浏览器 ...

  6. #Deep Learning回顾#之基于深度学习的目标检测(阅读小结)

    原文链接:https://www.52ml.net/20287.html 这篇博文主要讲了深度学习在目标检测中的发展. 博文首先介绍了传统的目标检测算法过程: 传统的目标检测一般使用滑动窗口的框架,主 ...

  7. linux kernel 平台总线实例分析

    linux 平台总线的实现有三大块  , platform bus , platform device , platform drvice 平台类型结构体: /** * struct bus_type ...

  8. 1.AngularJS初探

    1.需要什么前端开发环境 1)代码编辑工具 webstorm 2)断点调试工具 chrome插件Batarang 3)版本管理 tortoiseGit 4)代码合并和混淆工具 grunt-contri ...

  9. linux 命令free -m 命令结果分析

    free -m 命令详解 free -m 分析系统内存使用情况:

  10. Extjs String转Json

    var jsonStr= '{ "name": "zhangsan", "age": "18" }'; var json ...