php - Cakephp Events registered but not firing -


hi guy's many people i'm having dificulties implementing events cakephp application cool if point out messed im using cakephp v2.7 allso followed martin bean's tutorial on subject: http://martinbean.co.uk/blog/2013/11/22/getting-to-grips-with-cakephps-events-system/


my code follows: app/event/userlistener.php

<?php app::uses('cakeeventlistener', 'event');  class userlistener implements cakeeventlistener {  public function implementedevents() {     #@:off;     return array(         'model.user.test' => 'test',         'model.user.created' => 'sendactivationemail',     );     #@:on; }  public function test($event) {     ($i = 0; $i < 10; $i++) {         echo "string<br />";     }     cakelog::write('cakeevents', 'testevent fired'); }  public function sendactivationemail($event) {  }  } 

in app/model/user.php

<?php app::uses('appmodel', 'model'); app::uses('role', 'model'); app::uses('clroute', 'model'); app::uses('cakeevent', 'event');  public function afterfind($results, $primary = true) {     $event = new cakeevent('model.user.test', $this, array(     #@:off;             'id' => 66,             'data' => 'ppx'         )     );     #@:on;     cakelog::write('cakeevents', 'testevent dispatch in usermodel');     $this -> geteventmanager() -> dispatch($event);     if ($results[0]['salon']['id'] == null) {         $results[0]['salon'] = false;     }     return $results; } 

and last not least in app/config/bootstrap.php

app::uses('classregistry', 'utility'); app::uses('userlistener', 'event'); $user = classregistry::init('user'); $user->geteventmanager()->attach(new userlistener()); 

it great if me out because i'm stuck there , cakephp's documentation on subject kind of hazy allso :-/

big thx in advance!! regards michael

i advise against directly attaching event listeners in model (as suggested in own answer). defeats point of using events in first place. 1 of strengths of using events allows develop more extendable code can overridden; attaching listener directly model goes against concept.

if you're listener doesn't appear called despite attaching model in bootstrap.php may need globally attach it:-

// in app/config/bootstrap.php app::uses('cakeeventmanager', 'event'); app::uses('userlistener', 'lib/event'); cakeeventmanager::instance()->attach(new userlistener()); 

this instead of attaching directly user model.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -