zoukankan      html  css  js  c++  java
  • [Yii Framework] 再谈Yii Framework中的事件event

    再谈Yii Framework中的事件event,我写过的关于Yii事件event的另一篇文章
    [Yii Framework] Yii中事件和行为的区别和应用 http://www.cnblogs.com/davidhhuan/archive/2012/01/19/2326123.html

    假设有类MyComponent,它是继承于CComponent,通过查看 CComponent 的 __set() 方法, 

    public function __set($name,$value)
    {
        $setter='set'.$name;
        if(method_exists($this,$setter))
            return $this->$setter($value);
        else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
        {
            // duplicating getEventHandlers() here for performance
            $name=strtolower($name);
            if(!isset($this->_e[$name]))
                $this->_e[$name]=new CList;
            return $this->_e[$name]->add($value);
        }
        else if(is_array($this->_m))
        {
            foreach($this->_m as $object)
            {
                if($object->getEnabled() && (property_exists($object,$name) || $object->canSetProperty($name)))
                    return $object->$name=$value;
            }
        }
        if(method_exists($this,'get'.$name))
            throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.',
                array('{class}'=>get_class($this), '{property}'=>$name)));
        else
            throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
                array('{class}'=>get_class($this), '{property}'=>$name)));
    }

    第四行可知,我们可以通过 onXXX 来直接设置事件的。 

    绑定到全局事件处理
    方法一:

    直接在main.php里面定义 

    /***************************************************
    在我们想要的内容的前后出现了这些代码
    只是为了说明,我们添加的内容是要放在
    这个配置数据的一维里面。
    'import'=>array(
        'application.models.*',
        'application.components.*',
        'application.helpers.*',
    ),
    'defaultController'=>'post',
    **************************************************
    */

    //其它代码
    'import'=>array(
        'application.models.*',
        'application.components.*',
        'application.helpers.*',
    ),

    /************** 这才是我们想要添加的代码 **************/
    'onBeginRequest' => array('MyEventHandler', 'MyEventHandlerMethod'),

    'defaultController'=>'post',
    //其它代码

    方法二: 

    //参考自framework/logging/CLogRouter.php的init()方法
    Yii::app()->attachEventHandler('onEndRequest',array($this,'processLogs'));

    绑定到局部事件处理 

    随时随地无论在controller还是model里面,只要是CComponent的子类,都可以这样定义,

    $myComponent->onClick = $callback;
    这里的 $callback 指向了一个有效的 PHP 回调。它可以是一个全局函数也可以是类中的一个方法。

    如果是后者,它必须以一个数组的方式提供 : array($object,'methodName')。

    其它文章推荐:

    Yii分析2:组件的事件机制    http://luchuan.iteye.com/blog/910251 

    Have fun with Yii! 

  • 相关阅读:
    Web 自动化测试(Selenium) PO 模型
    Web 自动化测试(Selenium) 鼠标和键盘操作以及浏览器等待
    Web 自动化测试(Selenium)进阶及八大元素定位
    web 自动化测试(Selenium) Xpath 和 Css 定位元素
    没有最全,只有更全的正则表达式集合(持续更新...)
    SQL优化第一篇
    C# 设置桌面为父窗口
    Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL)
    IDEA2020.2版本设置类和方法的自定义注释模板
    记一个Java多线程相关的面试题
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/2605164.html
Copyright © 2011-2022 走看看