Cakephp Classroom image

Anil  Bist / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20|Last
Lessons:-Core Libraries

}
This may not be the cleanest way to do it, so you can come up with your own ways for attaching listeners
to an object’s event manager. This simple way of defining them using the Configure class is intended for
didactic purposes only. This little example allows us to showcase what type of callbacks can be attached to
the manager. As you may already have figured out, the attach method takes any valid PHP callback type, this
is a string representing a static function call, an array having a class instance and a method, an anonymous
function if you use PHP 5.3, etc. Attached callbacks will all receive the event object as first argument
CakeEventManager::attach() Accepts three arguments. The leftmost one is the callback itself,
anything that PHP can treat as a callable function. The second argument is the event name, and the callback
will only get fired if the CakeEvent object dispatched has a matching name. The last argument is an array of
options to configure the callback priority, and the preference of arguments to be passed.
Registering listeners Listeners are an alternative, and often cleaner way of registering callbacks for an
event. This is done by implementing the CakeEventListener interface in any class you wish to register
some callbacks. Classes implementing it need to provide the implementedEvents() method and return
an associative array with all event names that the class will handle.
To keep up with our previous example, let’s imagine we have a UserStatistic class responsible for calculating
useful information and compiling into the global site statistics. It would be natural to pass an instance of this
class as a callback, instead of implementing a custom static function or converting any other workaround to
trigger methods in this class. A listener is created as follows:
App::uses(’CakeEventListener’, ’Event’);
class UserStatistic implements CakeEventListener {
public function implementedEvents() {
return array(
’Model.Order.afterPlace’ => ’updateBuyStatistic’,
);
}
public function updateBuyStatistic($event) {
// Code to update statistics
}
}
// Attach the UserStatistic object to the Order’s event manager
$statistics = new UserStatistic();
$this->Order->getEventManager()->attach($statistics);
As you can see in the above code, the attach function can handle instances of the CakeEventListener interface.
Internally, the event manager will read the array returned by implementedEvents method and wire the
callbacks accordingly.
Establishing priorities In some cases you’d want to run a callback and make sure it gets executed before,
or after all the other callbacks have been run. For instance, think again about our user statistics example.
It would make sense to run this method only when we can make sure the event was not cancelled, there

 
 
 
image
Anil  Bist

Skills    Cakephp

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)