FuelPHP - Events



An event is an action or occurrence recognized by the program that may be handled by the program itself. For example, we may define an action or event named my_fuel_event and then do some work whenever the event, my_fuel_event is called. FuelPHP provides class, Event to work with the events in the application.

System Events

FuelPHP defined some of the event through which we can do some work whenever the defined events are called or fired by the application. This help is changing the behavior of the FuelPHP without changing the FuelPHP's core code files. The pre-defined events are as follows −

  • app_created − This event will be triggered after the FuelPHP framework has been initialized.

  • 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 is 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 sent.

We can handle the events in the special configuration file, fuel/app/config/events.php as follows −

<?php  
   return array ( 
      'fuelphp' => array ( 
         'app_created' => function() { 
            // After FuelPHP initialised
         }, 
         'request_created' => function() { 
            // After Request forged 
         }, 
         'request_started' => function() { 
            // Request is requested 
         }, 
         'controller_started' => function() { 
            // Before controllers before() method called 
         }, 
         'controller_finished' => function() { 
            // After controllers after() method called 
         }, 
         'response_created' => function() { 
            // After Response forged 
         }, 
         'request_finished' => function() { 
            // Request is complete and Response received 
         }, 
         'shutdown' => function() { 
            // Output has been send out 
         }, 
      ), 
   );

Event Methods

Event class provides methods to register, unregister, and fire events. They are as follows,

register()

The register method allows files to register an object that will be run when the trigger method is called.

$my_event_code = function() { 
   echo 'my event'; 
} 
Event::register('my_event', $my_event_code); 

unregister()

The unregister method allows the files to unregister an object that would be run when the trigger method is called.

Event::unregister('my_event', $my_event_code);

trigger()

The trigger method is used to trigger or activate callbacks that are associated through the register method.

Event::trigger('my_event');

has_events()

The has_events method is available so you can check if a particular registered event has triggers.

Event::has_events('my_event');

forge()

The forge returns a new event object.

$event = Event::forge();

instance()

The instance returns a new event object singleton.

$event = Event::instance('event_instance');
Advertisements