title: Where to register event listeners
body: <p>I'm trying to use the Event System in CakePHP v2.1+</p>
  
  <p>It appears to be quite powerful, but the documentation is somewhat vague. Triggering the event seems pretty straight-forward, but I'm not sure how to register the corresponding listener(s) to listen for the event. The relevant section is <a href="http://book.cakephp.org/2.0/en/core-libraries/events.html#registering-listeners" rel="noreferrer">here</a> and it offers the following example code:</p>
  
  <pre><code>App::uses('CakeEventListener', 'Event');
  class UserStatistic implements CakeEventListener {
  
      public function implementedEvents() {
          return array(
              'Model.Order.afterPlace' =&gt; 'updateBuyStatistic',
          );
      }
  
      public function updateBuyStatistic($event) {
          // Code to update statistics
      }
  }
  
  // Attach the UserStatistic object to the Order's event manager
  $statistics = new UserStatistic();
  $this-&gt;Order-&gt;getEventManager()-&gt;attach($statistics);
  </code></pre>
  
  <p>But it does not say <em>where</em> this code should reside. Inside a specific controller? Inside the app controller?</p>
  
  <p>In case it's relevant, the listener will be part of a plugin which I am writing. </p>
  
  <p><strong>Update:</strong>
  It sounds like a popular way to do this is by placing the listener registration code in the plugin's bootstrap.php file. However, I can't figure out how to call getEventManager() from there because the app's controller classes, etc aren't available.</p>
  
  <p><strong>Update 2:</strong>
  I'm also told that listeners can live inside Models.</p>
  
  <p><strong>Update 3:</strong>
  Finally some traction! The following code will successfully log an event when inside of my MyPlugin/Config/bootstrap.php</p>
  
  <pre><code>App::uses('CakeEventManager', 'Event'); 
  App::uses('CakeEventListener', 'Event');
  class LegacyWsatListener implements CakeEventListener {
  
      public function implementedEvents() {
          return array(
              'Controller.Attempt.complete' =&gt; 'handleLegacyWsat',
          );
      }
  
      public static function handleLegacyWsat($event) { //method must be static if used by global EventManager
          // Code to update statistics
          error_log('event from bootstrap');
      }
  }
   CakeEventManager::instance()-&gt;attach(array('LegacyWsatListener', 'handleLegacyWsat'), 'Controller.Attempt.complete');
  </code></pre>
  
  <p>I'm not sure why, but I can't get errors when I try to combine the two <code>App::uses()</code> into a single line.</p>