Cakephp Classroom image

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

new CakeEvent(’Model.Order.afterPlace’, $this, array(
’order’ => $order
));
CakeEvent receives 3 arguments in its constructor. The first one is the event name, you should
try to keep this name as unique as possible, while making it readable. We suggest a convention
as follows: Layer.eventName for general events happening at a layer level (e.g. Controller.startup,
View.beforeRender) and Layer.Class.eventName for events happening in specific classes on a layer, for example
Model.User.afterRegister or Controller.Courses.invalidAccess.
The second argument is the subject, meaning the object associated to the event, usually when it is the same
class triggering events about itself, using $this will be the most common case. Although a Component
could trigger controller events too. The subject class is important because listeners will get immediate
access to the object properties and have the chance to inspect or change them on the fly.
Finally, the third argument is the event’s params. This can be any data you consider useful to pass around so
listeners can act upon it. While this can be an argument of any type, we recommend passing an associative
array, to make inspection easier.
CakeEventManager::dispatch() method accepts the event object as argument and notifies all listener
and callbacks passing this object along. So the listeners will handle all the extra logic around the
afterPlace event, you can log the time, send emails, update user statistics possibly in separate objects and
even delegating it to offline tasks if you have the need.
Registering callbacks How do we register callbacks or observers to our new afterPlace event?
This is subject to a wide variety of different implementations, but they all have to call the
CakeEventManager::attach() method to register new actors. For simplicity’s sake, let’s imagine
we know in the plugin what the callbacks are available in the controller, and say this controller is responsible
for attaching them. The possible code would look like this:
// Listeners configured somewhere else, maybe a config file:
Configure::write(’Order.afterPlace’, array(
’email-sending’ => ’EmailSender::sendBuyEmail’,
’inventory’ => array($this->InventoryManager, ’decrement’),
’logger’ => function($event) {
// Anonymous function are only available in PHP 5.3+
CakeLog::write(’info’, ’A new order was placed with id: ’ . $event->subject()->id);
}
));
// Cart/Controller/OrdersController.php
class OrdersController extends AppController {
public function finish() {
foreach (Configure::read(’Order.afterPlace’) as $l) {
$this->Order->getEventManager()->attach($l, ’Model.Order.afterPlace’);
}
if ($this->Order->place($this->Cart->items())) {
// ...
}
}

 
 
 

osdyui

Skills    Cakephp

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)