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'],'參數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

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)

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"

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

As the demand for web applications continues to grow, developers have more and more choices in choosing development frameworks. Symfony and Yii2 are two popular PHP frameworks. They both have powerful functions and performance, but when faced with the need to develop large-scale web applications, which framework is more suitable? Next we will conduct a comparative analysis of Symphony and Yii2 to help you make a better choice. Basic Overview Symphony is an open source web application framework written in PHP and is built on

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

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:
