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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
d5329954
Commit
d5329954
authored
May 25, 2014
by
Larry Ullman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Edited up to event handlers
parent
caba264d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
9 deletions
+9
-9
concept-events.md
docs/guide/concept-events.md
+9
-9
No files found.
docs/guide/concept-events.md
View file @
d5329954
...
...
@@ -3,18 +3,18 @@ Events
Events allow you to inject custom code into existing code at certain execution points. You can attach custom
code to an event so that when the event is triggered, the code gets executed automatically. For example,
a mailer object may trigger a
`messageSent`
event when it successfully sends
out
a message. If you want to keep
track of the messages that are successfully sent, you
ma
y attach the tracking code to the
`messageSent`
event.
a mailer object may trigger a
`messageSent`
event when it successfully sends a message. If you want to keep
track of the messages that are successfully sent, you
could then simpl
y attach the tracking code to the
`messageSent`
event.
Yii introduces a base class called
[
[yii\base\Component
]
] to support events. If a class needs to trigger
events, it should extend from
[
[yii\base\Component
]
] or
its
child class.
events, it should extend from
[
[yii\base\Component
]
] or
a
child class.
Triggering Events <a name="triggering-events"></a>
-----------------
Events are triggered by calling the
[
[yii\base\Component::trigger()
]
] method. The method requires an
*event name*
and optionally an event object
which describes the parameters to be passed to the event handlers. For example,
Events are triggered by calling the
[
[yii\base\Component::trigger()
]
] method. The method requires an
*event name*
,
and optionally an event object
that describes the parameters to be passed to the event handlers. For example:
```
php
namespace
app\components
;
...
...
@@ -36,15 +36,15 @@ class Foo extends Component
In the above code, when you call
`bar()`
, it will trigger an event named
`hello`
.
> Tip: It is recommended to use class constants to represent event names. In the above example, the constant
`EVENT_HELLO`
is used to represent
`hello`
. This
has two benefits. First, it prevents typos and can ge
t IDE
`EVENT_HELLO`
is used to represent
`hello`
. This
approach has two benefits. First, it prevents typos and can impac
t IDE
auto-completion support. Second, you can tell what events are supported by a class by simply checking the constant
declarations.
Sometimes when triggering an event, you may want to pass along
some
additional information to the event handlers.
Sometimes when triggering an event, you may want to pass along additional information to the event handlers.
For example, a mailer may want pass the message information to the handlers of the
`messageSent`
event so that the handlers
can know
what messages are sent
. To do so, you can provide an event object as the second parameter to
can know
the particulars of the sent messages
. To do so, you can provide an event object as the second parameter to
the
[
[yii\base\Component::trigger()
]
] method. The event object must be an instance of the
[
[yii\base\Event
]
] class
or
its child class. For example,
or
a child class. For example:
```
php
namespace
app\components
;
...
...
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