Event Class
The event class allows you to interact with the Fuel Core without having to alter any core files.
The FuelPHP defines several events on the fuelphp instance, which you can use to hook into the
core without having to extend it. These events are:
app_created |
This event will be triggered after the FuelPHP framework has been initialised.
|
request_created |
This event will be triggered after a new Request object has been forged.
|
request_started |
This event will be triggered when execution of a Request is requested.
|
controller_started |
This event will be triggered before the controllers before() method is called.
|
controller_finished |
This event will be triggered after the controllers after() method has been called and the response received.
|
response_created |
This event will be triggered after a new Response object has been forged.
|
request_finished |
This event will be triggered when execution of a Request is complete and a response is received.
|
shutdown |
This event will be triggered after the main request has been processed and the output has been send out.
|
|
Example |
In app/config/event.php
<?php
return array(
'fuelphp' => array(
'app_created' => function()
{
},
'request_created' => function()
{
},
'request_started' => function()
{
},
'controller_started' => function()
{
},
'controller_finished' => function()
{
},
'response_created' => function()
{
},
'request_finished' => function()
{
},
'shutdown' => function()
{
},
),
);
|
The register method allows files to register an object that will be run when the trigger method is called.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$event |
required |
The event you are registering your code to. |
$callback |
required |
The callback method. |
|
Returns |
boolean |
Example |
Event::register('user_login', 'Class::method');
|
The unregister method allows files to unregister an object that would be run when the trigger method is called.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$event |
required |
The event you are registering your code to. |
$callback |
optional |
The callback method. If none provided all callbacks will be removed. |
|
Returns |
boolean |
Example |
$callback_one = function()
{
echo 'callback one';
}
$callback_two = function()
{
echo 'callback two';
}
Event::register('my_event', $callback_one);
Event::register('my_event', $callback_two);
Event::unregister('my_event', $callback_one);
Event::trigger('my_event');
Event::unregister('my_event');
Event::trigger('my_event');
|
The trigger method is used to trigger or activate callbacks that are associated through the register method.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$event |
required |
The event you are triggering. |
$data |
''
|
Any data you want to send to the method registered. |
$return_type |
'string'
|
What type of return data you expect. (string, array, json, none, serialized) - Default is string |
$reversed |
false
|
Set to true to fire the events in reversed order, LIFO instead or FIFO. |
|
Returns |
mixed - Dependent on the $return_type. |
Example |
Event::trigger('user_login', $data)
|
The has_events method is available so you can check if a particular registered event has triggers.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$event |
required |
The event you are checking. |
|
Returns |
boolean |
Example |
Event::has_events('user_login')
|
The forge returns a new event object.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$events |
array()
|
The event you are checking. |
|
Returns |
Event_Instance object |
Example |
$events = Event::forge();
$events = Event::forge(array(
'update' => function(){
},
'register' => function(){
},
));
$events->register('my_event', function(){
echo 'this is awesome';
});
$events->trigger('my_event');
|
The instance returns a new event object singleton.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$name |
'fuelphp'
|
The instance name. |
$events |
array()
|
The event you are checking. |
|
Returns |
Event_Instance object |
Example |
$events = Event::instance('my_instance');
$same = Event::instance('my_instance');
$events->register('my_event', function(){
echo 'awesome!';
});
$same->register('my_event', function(){
echo 'this is ';
});
Event::instance('my_instance')->trigger('my_event');
|