亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home PHP Framework YII How to bind events in yii2.0

How to bind events in yii2.0

Feb 17, 2020 pm 02:20 PM
yii event

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'; //發(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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Get upcoming calendar events on your iPhone lock screen Get upcoming calendar events on your iPhone lock screen Dec 01, 2023 pm 02:21 PM

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

How to build event-based applications using PHP How to build event-based applications using PHP May 04, 2024 pm 02:24 PM

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.

How to implement change event binding of select elements in jQuery How to implement change event binding of select elements in jQuery Feb 23, 2024 pm 01:12 PM

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:

What is the difference between php framework laravel and yii What is the difference between php framework laravel and yii Apr 30, 2025 pm 02:24 PM

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? How to implement calendar functions and event reminders in PHP projects? Nov 02, 2023 pm 12:48 PM

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

Yii with Docker: Containerizing and Deploying Your Applications Yii with Docker: Containerizing and Deploying Your Applications Apr 02, 2025 pm 02:13 PM

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.

A Deep Dive into Close Button Events in jQuery A Deep Dive into Close Button Events in jQuery Feb 24, 2024 pm 05:09 PM

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

Master the common event bubbling mechanism in JavaScript Master the common event bubbling mechanism in JavaScript Feb 19, 2024 pm 04:43 PM

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

See all articles