Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
yii2.0怎么绑定事件_创想鸟

yii2.0怎么绑定事件

yii2.0怎么绑定事件

%ignore_a_1%2中,事件的绑定是通过yiibasecomponent的on方法进行操作的,我们在定义事件的同时,需要为其绑定一个回调函数

看下例子,先写下一个控制器,用on绑定事件,然后在方法里面用triggle调用

namespace backendcontrollers;use yiiwebController;class EventController extends Controller{   const TEST_EVENT = 'event';    public function init()    {        parent::init();        $this->on(self::TEST_EVENT,function(){echo '这个一个事件测试。。。';});    }    public function actionIndex()    {        $this->trigger(self::TEST_EVENT);    }}

访问index方法后得到事件的结果。在进入控制器的时候就给‘event’绑定了一个时间,on第一个参数表示事件名(必须是常量),第二个参数是这个事件的回调函数。

(推荐教程:yii框架)

也可以写成如下的方式:

namespace backendcontrollers;use yiiwebController;class EventController extends Controller{   const TEST_EVENT = 'event';    public function init()    {        parent::init();        $this->on(self::TEST_EVENT,[$this,'onTest']);    }    public function onTest()    {        echo '这个一个事件测试。。。';    }    public function actionIndex()    {        $this->trigger(self::TEST_EVENT);    }}

$this表示的是本对象,‘onTest’指的是执行的方法。事件绑定好后没有调用还是没用,此时用到yiibaseCompontent类中的triggle方法来调用了。

事件的扩展运用(参数的传入方法)

先定义一个控制器在里面定义加调用,如果想要传入不同的参数就要用到 yiibaseEvent 类了

class EventController extends Controller{    const TEST_USER = 'email'; //发送邮件    public function init()    {        parent::init();        $msg = new Msg();        $this->on(self::TEST_USER,[$msg,'Ontest'],'参数Test');      }    public function actionTest()    {        $msgEvent = new MsgEvent();        $msgEvent->dateTime = 'Test时间';        $msgEvent->author = 'Test作者';        $msgEvent->content = 'Test内容';        $this->trigger(self::TEST_USER,$msgEvent);    }}
class MsgEvent extends Event{    public $dateTime;   // 时间    public $author;     // 作者    public $content;    // 内容}

msg里面放的是调用的方法

class Msg extends ActiveRecord{    public function onTest($event) //$event是yiibaseEvent的对象    {        print_r($event->author);//输出'Test作者'        print_r($event->dateTime);//输出'Test时间'        print_r($event->content);//输出'Test内容'        print_r($event->data);//输出'参数Test'    }}

更多编程相关内容学习,请访问创想鸟编程教程栏目!

以上就是yii2.0怎么绑定事件的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/96802.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
sublime怎么在mac上通过命令行启动_Mac系统通过命令行启动Sublime设置教程
上一篇 2025年11月20日 00:52:46
淘宝商品详情加载慢怎么办
下一篇 2025年11月20日 00:54:48

相关推荐

发表回复

登录后才能评论
关注微信