- Gii – Generating Module
- Gii – Generating Controller
- Gii – Creating a Model
- Yii - Gii
- Yii - Localization
- Yii - Authorization
- Yii - Authentication
- Yii - Error Handling
- Yii - Logging
- Yii - Aliases
- Yii - Fragment Caching
- Yii - Caching
- Yii - Testing
- Yii - Fields
- Yii - RESTful APIs in Action
- Yii - RESTful APIs
- Yii - Theming
- Yii - Database Migration
- Yii - Active Record
- Yii - Query Builder
- Yii - Data Access Objects
- Yii - Database Access
- Yii - Dependency Injection
- Yii - Configurations
- Yii - Creating a Behavior
- Yii - Behaviors
- Yii - Creating Event
- Yii - Events
- Yii - GridView Widget
- Yii - ListView Widget
- Yii - Data Widgets
- Yii - Data Providers
- Yii - Properties
- Yii - Sorting
- Yii - Pagination
- Yii - Formatting
- Yii - Files Upload
- Yii - Using Cookies
- Yii - Cookies
- Yii - Using Flash Data
- Yii - Sessions
- Yii - AJAX Validation
- Yii - Ad Hoc Validation
- Yii - Validation
- Yii - HTML Forms
- Yii - Rules of URL
- Yii - URL Routing
- Yii - URL Formats
- Yii - Responses
- Yii - HTTP Requests
- Yii - Creating Extensions
- Yii - Extensions
- Yii - Asset Conversion
- Yii - Assets
- Yii - Layouts
- Yii - Views
- Yii - Modules
- Yii - Widgets
- Yii - Models
- Yii - Using Actions
- Yii - Using Controllers
- Yii - Controllers
- Yii - Entry Scripts
- Yii - Application Structure
- Yii - Create Page
- Yii - Installation
- Yii - Overview
- Yii - Home
Yii Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Yii - Events
You can use events to inject custom code at certain execution points. You can attach custom code to an event, and when the event is triggered, the code gets executed. For example, a logger object may trigger a userRegistered event when a new user registers on your web site. If a class needs to trigger events, you should extend it from the yiiaseComponent class.
An event handler is a PHP callback. You can use the following callbacks −
A global PHP function specified as a string.
An anonymous function.
An array of a class name and a method as a string, for example, [ ClassName , methodName ]
An array of an object and a method as a string, for example, [$obj, methodName ]
Step 1 − To attach a handler to an event you should call the yiiaseComponent::on() method.
$obj = new Obj; // this handler is a global function $obj->on(Obj::EVENT_HELLO, function_name ); // this handler is an object method $obj->on(Obj::EVENT_HELLO, [$object, methodName ]); // this handler is a static class method $obj->on(Obj::EVENT_HELLO, [ appcomponentsMyComponent , methodName ]); // this handler is an anonymous function $obj->on(Obj::EVENT_HELLO, function ($event) { // event handpng logic });
You can attach one or more handlers to an event. The attached handlers are called in the order they were attached to the event.
Step 2 − To stop in the invocation of the handlers, you should set the yiiaseEvent::$handled property to true.
$obj->on(Obj::EVENT_HELLO, function ($event) { $event->handled = true; });
Step 3 − To insert the handler at the start of the queue, you may call yiiaseComponent::on(), passing false for the fourth parameter.
$obj->on(Obj::EVENT_HELLO, function ($event) { // ... }, $data, false);
Step 4 − To trigger an event, call the yiiaseComponent::trigger() method.
namespace appcomponents; use yiiaseComponent; use yiiaseEvent; class Obj extends Component { const EVENT_HELLO = hello ; pubpc function triggerEvent() { $this->trigger(self::EVENT_HELLO); } }
Step 5 − To detach a handler from an event, you should call the yiiaseComponent::off() method.
$obj = new Obj; // this handler is a global function $obj->off(Obj::EVENT_HELLO, function_name ); // this handler is an object method $obj->off(Obj::EVENT_HELLO, [$object, methodName ]); // this handler is a static class method $obj->off(Obj::EVENT_HELLO, [ appcomponentsMyComponent , methodName ]); // this handler is an anonymous function $obj->off(Obj::EVENT_HELLO, function ($event) { // event handpng logic });Advertisements