zoukankan      html  css  js  c++  java
  • Cakephp事件机制尝鲜

    cakephp 很老的框架了,尝鲜算是牵强,不过就算很先进的框架例如YII都有这些机制,不过仍然没有理解

    手册地址: http://book.cakephp.org/2.0/en/core-libraries/events.html

    使用方式:

        

    1、在需要支持事件的类中(包括、模型、控制器等)使用CakeEventListener 接口并且,实现接口规定的所有方法。

    <?php App::uses( 'AppModel', 'Model' ); App::uses('CakeEventListener', 'Event'); class COrder extends AppModel implements CakeEventListener { public function implementedEvents() { return array( 'Model.COrder.test' =>array( 'callable'=> 'eventTest', 'passParams'=>true ), ); } public function eventTest($order) { //TODO pr($order);exit(); } }
    2、注册事件,注册事件可以在APPController中操作,全局注册所有的事件       
    // 注册事件 $this->loadModel("COrder"); $this->getEventManager()->attach($this->COrder);
    3、触发事件,在系统任意一个地方都可以使用触发事件机制,使用方法如下      
    //触发事件 $event = new CakeEvent('Model.COrder.test', $this, array( 'order' =>array('a') )); $this->getEventManager()->dispatch($event);

     心得:使用事件机制可以在一定程度上解耦,不必每次都要去引入需要的额对象,例如在模型里面引入其他模型是不合适且不方便的,但是PHP的事件机制造成了程序代码不利于跟踪,程序可读性被破坏,同时也不能很好地进行事务操作。所以请谨慎使用cakePHP的事件机制。

  • 相关阅读:
    requests访问https网站
    BurpSuite中的安全测试插件推荐
    requests.exceptions.SSLError: hostname '127.0.0.1' doesn't match None
    Flask如何使用https?
    sonar如何添加自定义JAVA规则
    如何破解linux用户帐号密码一
    linux下编译运行C程序
    sp_Msforeachtable与sp_Msforeachdb详解
    sp_MSforeachtable使用方法
    SQL Server 存储过程
  • 原文地址:https://www.cnblogs.com/linksgo2011/p/3977546.html
Copyright © 2011-2022 走看看