Cakephp Classroom image

Anil  Bist / Professional / Web Technology

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

some developers chose to implement generic event systems to solve this problem, and some of those system
were offered as plugins. Now, you can benefit from a standard general purpose event system that will let
you cleanly separate the concerns of your plugins and application code with the built in events manager.
Dispatching events So back to our example, we would have an Order model that will manage the buying
logic, and probably a place method to save the order details and do any other logic:
// Cart/Model/Order.php
class Order extends AppModel {
public function place($order) {
if ($this->save($order)) {
$this->Cart->remove($order);
$this->sendNotificationEmail();
$this->decrementFromStock();
$this->updateUserStatistics();
// ...
return true;
}
return false;
}
}
Well, that does not look right at all. A plugin should not make any assumption about sending emails, and
may not even have the inventory data to decrement the item from it, and definitely tracking usage statistics
is not the best place to do it. So we need another solution, let’s rewrite that using the event manager:
// Cart/Model/Order.php
App::uses(’CakeEvent’, ’Event’);
class Order extends AppModel {
public function place($order) {
if ($this->save($order)) {
$this->Cart->remove($order);
$this->getEventManager()->dispatch(new CakeEvent(’Model.Order.afterPlace’, $this, ’order’ => $order
)));
return true;
}
return false;
}
}
That looks a lot cleaner, at gives us the opportunity to introduce the event classes and methods. The first
thing you may notice is the call to getEventManager() this is a method that is available by default in all
Models, Controller and Views. This method will not return the same manager instance across models, and it
is not shared between controllers and models, but they are between controllers and views, nevertheless. We
will review later how to overcome this implementation detail.
The getEventManager method returns an instance of CakeEventManager, and to dispatch events
you use CakeEventManager::dispatch() which receives an instance of the CakeEvent class.
Let’s dissect now the process of dispatching an event:

 
 
 
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)