国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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 '這個(gè)一個(gè)事件測(cè)試。。。';});
    }


    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 '這個(gè)一個(gè)事件測(cè)試。。。';
    }

    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時(shí)間';
        $msgEvent->author = 'Test作者';
        $msgEvent->content = 'Test內(nèi)容';
        $this->trigger(self::TEST_USER,$msgEvent);
    }
}
class MsgEvent extends Event
{
    public $dateTime;   // 時(shí)間
    public $author;     // 作者
    public $content;    // 內(nèi)容

}

msg contains the calling method

class Msg extends ActiveRecord
{
    public function onTest($event) //$event是yii\base\Event的對(duì)象
    {
        print_r($event->author);//輸出'Test作者'
        print_r($event->dateTime);//輸出'Test時(shí)間'
        print_r($event->content);//輸出'Test內(nèi)容'
        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)

Event ID 4660: Object deleted [Fix] Event ID 4660: Object deleted [Fix] Jul 03, 2023 am 08:13 AM

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

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

In JavaScript, what is the purpose of the 'oninput' event? In JavaScript, what is the purpose of the 'oninput' event? Aug 26, 2023 pm 03:17 PM

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&quot

Data query in Yii framework: access data efficiently Data query in Yii framework: access data efficiently Jun 21, 2023 am 11:22 AM

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

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

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

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:

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

See all articles