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'; //發(fā)送郵件 public function init() { parent::init(); $msg = new Msg(); $this->on(self::TEST_USER,[$msg,'Ontest'],'參數(shù)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);//輸出'參數(shù)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

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

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

Methods for building event-based applications in PHP include using the EventSourceAPI to create an event source and using the EventSource object to listen for events on the client side. Send events using Server Sent Events (SSE) and listen for events on the client side using an XMLHttpRequest object. A practical example is to use EventSource to update inventory counts in real time in an e-commerce website. This is achieved on the server side by randomly changing the inventory and sending updates, and the client listens for inventory updates through EventSource and displays them in real time.

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:

The main differences between Laravel and Yii are design concepts, functional characteristics and usage scenarios. 1.Laravel focuses on the simplicity and pleasure of development, and provides rich functions such as EloquentORM and Artisan tools, suitable for rapid development and beginners. 2.Yii emphasizes performance and efficiency, is suitable for high-load applications, and provides efficient ActiveRecord and cache systems, but has a steep learning curve.

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

The steps to containerize and deploy Yii applications using Docker include: 1. Create a Dockerfile and define the image building process; 2. Use DockerCompose to launch Yii applications and MySQL database; 3. Optimize image size and performance. This involves not only specific technical operations, but also understanding the working principles and best practices of Dockerfile to ensure efficient and reliable deployment.

In-depth understanding of the close button event in jQuery During the front-end development process, we often encounter situations where we need to implement the close button function, such as closing pop-up windows, closing prompt boxes, etc. When using jQuery, a popular JavaScript library, it becomes extremely simple and convenient to implement the close button event. This article will delve into how to use jQuery to implement close button events, and provide specific code examples to help readers better understand and master this technology. First, we need to understand how to define

Common bubbling events in JavaScript: To master the bubbling characteristics of common events, specific code examples are required. Introduction: In JavaScript, event bubbling means that the event will start from the element with the deepest nesting level and propagate to the outer element until it propagates to The outermost parent element. Understanding and mastering common bubbling events can help us better handle user interaction and event handling. This article will introduce some common bubbling events and provide specific code examples to help readers better understand. 1. Click event (click
