How to bind events in yii2.0
In yii2, event binding is operated through the on method of yii\base\Component. When we define the event, we need to bind a callback function to it.
Look at the example, first write a controller, use on to bind the event, and then use triggle in the method to call
namespace backend\controllers; use yii\web\Controller; 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); } }
to access the index method and get the result of the event. When entering the controller, a time is bound to 'event'. The first parameter of on represents the event name (must be a constant), and the second parameter is the callback function of this event.
(Recommended tutorial: yii framework)
can also be written in the following way:
namespace backend\controllers; use yii\web\Controller; 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 represents this object, 'onTest' Refers to the method of execution. After the event is bound, it is useless if it is not called. At this time, the triggle method in the yii\base\Component class is used to call it.
Extended application of events (parameter passing method)
First define a controller and define and call it. If you want to pass in different parameters, you must The yii\base\Event class is used
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 contains the calling method
class Msg extends ActiveRecord { public function onTest($event) //$event是yii\base\Event的对象 { print_r($event->author);//输出'Test作者' print_r($event->dateTime);//输出'Test时间' print_r($event->content);//输出'Test内容' print_r($event->data);//输出'参数Test' } }
For more programming related content, please visit the php Chinese websiteProgramming Tutorialcolumn!
The above is the detailed content of How to bind events in yii2.0. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics


![Event ID 4660: Object deleted [Fix]](https://img.php.cn/upload/article/000/887/227/168834320512143.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Some of our readers encountered event ID4660. They're often not sure what to do, so we explain it in this guide. Event ID 4660 is usually logged when an object is deleted, so we will also explore some practical ways to fix it on your computer. What is event ID4660? Event ID 4660 is related to objects in Active Directory and will be triggered by any of the following factors: Object Deletion – A security event with Event ID 4660 is logged whenever an object is deleted from Active Directory. Manual changes – Event ID 4660 may be generated when a user or administrator manually changes the permissions of an object. This can happen when changing permission settings, modifying access levels, or adding or removing people or groups

On iPhones running iOS 16 or later, you can display upcoming calendar events directly on the lock screen. Read on to find out how it's done. Thanks to watch face complications, many Apple Watch users are used to being able to glance at their wrist to see the next upcoming calendar event. With the advent of iOS16 and lock screen widgets, you can view the same calendar event information directly on your iPhone without even unlocking the device. The Calendar Lock Screen widget comes in two flavors, allowing you to track the time of the next upcoming event, or use a larger widget that displays event names and their times. To start adding widgets, unlock your iPhone using Face ID or Touch ID, press and hold

When a value is added to the input box, the oninput event occurs. You can try running the following code to understand how to implement oninput events in JavaScript - Example<!DOCTYPEhtml><html> <body> <p>Writebelow:</p> <inputtype="text"

How to implement calendar functions and event reminders in PHP projects? Calendar functionality and event reminders are one of the common requirements when developing web applications. Whether it is personal schedule management, team collaboration, or online event scheduling, the calendar function can provide convenient time management and transaction arrangement. Implementing calendar functions and event reminders in PHP projects can be completed through the following steps. Database design First, you need to design a database table to store information about calendar events. A simple design could contain the following fields: id: unique to the event

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

Commonly used events in jquery are: 1. Window events; 2. Mouse events, which are events generated when the user moves or clicks the mouse on the document, including mouse clicks, move-in events, move-out events, etc.; 3. Keyboard events, Events are generated every time the user presses or releases a key on the keyboard, including key press events, key release events, etc.; 4. Form events, such as the focus() event will be triggered when an element gains focus, and the focus() event will be triggered when it loses focus. The blur() event is triggered, and the submit() event is triggered when the form is submitted.

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

As the Internet continues to develop, the demand for web application development is also getting higher and higher. For developers, developing applications requires a stable, efficient, and powerful framework, which can improve development efficiency. Yii is a leading high-performance PHP framework that provides rich features and good performance. Yii3 is the next generation version of the Yii framework, which further optimizes performance and code quality based on Yii2. In this article, we will introduce how to use Yii3 framework to develop PHP applications.
