Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
c259f4e0
Commit
c259f4e0
authored
Apr 25, 2014
by
Larry Ullman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Edited rest of doc
parent
04fb68fb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
11 deletions
+18
-11
events.md
docs/guide/events.md
+18
-11
No files found.
docs/guide/events.md
View file @
c259f4e0
...
...
@@ -82,7 +82,9 @@ When attaching event handlers in this way, the handler must be an anonymous func
Triggering events
-----------------
Any component can trigger an event using the
`trigger`
method:
Most events will be triggered through the normal workflow. For example, the "beforeSave" event occurs before an Active Record model is saved.
But you can also manually trigger an event using the
`trigger`
method, invoked on the component with the attached event handler:
```
php
$this
->
trigger
(
'myEvent'
);
...
...
@@ -94,8 +96,8 @@ $event->userName = 'Alexander';
$this
->
trigger
(
'createUserEvent'
,
$event
);
```
Event name should be unique within the class it is defined at. Event names are
*case-sensitive*
. I
t is a good practice
to define event names
using
class constants:
The event name needs to be unique within the class it is defined. Event names are
*case-sensitive*
, bu i
t is a good practice
to define event names
as
class constants:
```
php
class
Mailer
extends
Component
...
...
@@ -113,37 +115,42 @@ class Mailer extends Component
Removing Event Handlers
-----------------------
The correspond
o
ing
`off`
method removes an event handler:
The corresponding
`off`
method removes an event handler:
```
php
$component
->
off
(
$eventName
);
```
Yii supports the ability to associate multiple handlers with the same event. When using
`off`
as in the above,
every handler
is
removed. To remove only a specific handler, provide that as the second argument to
`off`
:
every handler
will be
removed. To remove only a specific handler, provide that as the second argument to
`off`
:
```
php
$component
->
off
(
$eventName
,
$handler
);
```
The
`$handler`
should be presented in the
`off`
method in the same way as was presented in
`on`
in order to remove it.
The
`$handler`
should be presented in the
`off`
method in the same way as was presented in the
`on`
call in order to remove it.
> Tip: You probably don't want to use anonymous functions for event handlers that you expect to later remove.
Global Events
-------------
You can use "global" events instead of per-component ones. To trigger a global event use an application instance instead
of specific component:
You can use "global" events instead of per-component ones. A global event can take place on any component type.
In order to attach a handler to a global event, call the
`on`
method on the application instance:
```
php
Yii
::
$app
->
trigger
(
$eventName
);
Yii
::
$app
->
on
(
$eventName
,
$handler
);
```
In order to attach a handler to it use the following:
Global events are triggered on the application instance instead
of a specific component:
```
php
Yii
::
$app
->
on
(
$eventName
,
$handler
);
Yii
::
$app
->
trigger
(
$eventName
);
```
Class Events
------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment