title: ZF2 getServiceLocator in ControllerPlugin class
body: <p>I am trying to get service locator/entity manager in plugin class, How can I get that. </p>
  
  <p>In my controller I am getting it like this. </p>
  
  <pre><code>public function getEntityManager()
  {
      if(null === $this-&gt;em){
          $this-&gt;em = $this-&gt;getServiceLocator()-&gt;get('doctrine.entitymanager.orm_default');
      }
      return $this-&gt;em;
  }
  
  public function setEntityManager(EntityManager $em)
  {
      $this-&gt;em = $em;
  }
  </code></pre>
  
  <p>but in plugin class I am getting error  on $this->getServiceLocator() line. because this is not available in plugin class. </p>
  
  <p>How can I do the same so that I can fetch some records and insert few in database in plugin. </p>
  
  <p>I do have MvcEvent $e object in my plugin class, I can make use of this to get entity manager?</p>
  
  <p>I have used <a href="http://samsonasik.wordpress.com/2012/08/23/zend-framework-2-controllerpluginmanager-append-controller-pluginto-all-controller/" rel="nofollow">this plugin</a> to create my plugin</p>
  
  <p>Any guide will be appriciated.</p>
  
  <p>update:</p>
  
  <pre><code>namespace Auth\Controller\Plugin;
  
  use Zend\Mvc\Controller\Plugin\AbstractPlugin;
  use Zend\EventManager\EventInterface as Event;
  use Zend\Authentication\AuthenticationService;
  
  use Doctrine\ORM\EntityManager;
  use Auth\Entity\User;
  use Zend\Mvc\MvcEvent;
  class AclPlugin extends AbstractPlugin
  {
  
      /*
      * @var Doctrine\ORM\EntityManager
      */
      protected $em;
  
      public function checkAcl($e)
      {
  
          $auth = new AuthenticationService();
          if ($auth-&gt;hasIdentity()) {
              $storage = $auth-&gt;getStorage()-&gt;read();            
              if (!empty($storage-&gt;role))
                  $role = strtolower ( $storage-&gt;role );            
              else 
                  $role = "guest";            
          } else {
              $role = "guest";            
          }
          $app = $e-&gt;getParam('application');
          $acl          = new \Auth\Acl\AclRules(); 
  
          $matches      = $e-&gt;getRouteMatch();
          $controller   = $matches-&gt;getParam('controller');
          $action       = $matches-&gt;getParam('action', 'index');
  
          $resource = strtolower( $controller );
          $permission = strtolower( $action );
  
          if (!$acl-&gt;hasResource($resource)) {
              throw new \Exception('Resource ' . $resource . ' not defined');
          }
  
          if ($acl-&gt;isAllowed($role, $resource, $permission)) {
  
              $query = $this-&gt;getEntityManager($e)-&gt;createQuery('SELECT u FROM Auth\Entity\User u');
              $resultIdentities = $query-&gt;execute();
  
              var_dump($resultIdentities);
              exit();
  
  
              return;
  
          } else {
              $matches-&gt;setParam('controller', 'Auth\Controller\User'); // redirect
              $matches-&gt;setParam('action', 'accessdenied');    
  
              return;
          }
  
  
      }
  
  
      public function getEntityManager($e) {
  
          var_dump($this-&gt;getController()); // returns null
          exit();
          if (null === $this-&gt;em) {
              $this-&gt;em = $this-&gt;getController()-&gt;getServiceLocator()-&gt;get('doctrine.entitymanager.orm_default');
  
          }
          return $this-&gt;em;
      }
  
      public function setEntityManager(EntityManager $em) {
          $this-&gt;em = $em;
      }
  
  }
  </code></pre>
  
  <p>I am calling above class in module.php</p>
  
  <pre><code>    public function onBootstrap(Event $e) 
      {
  
          $application = $e-&gt;getApplication();        
          $services    = $application-&gt;getServiceManager();        
  
          $eventManager = $e-&gt;getApplication()-&gt;getEventManager();
          $eventManager-&gt;attach('dispatch', array($this, 'loadConfiguration'),101);
  
      }
  
  public function loadConfiguration(MvcEvent $e)
      {
  
          $e-&gt;getApplication()-&gt;getServiceManager()
                    -&gt;get('ControllerPluginManager')-&gt;get('AclPlugin')
                    -&gt;checkAcl($e); //pass to the plugin...      
  
      }
  </code></pre>
  
  <p>I am registering this plugin in module.config.php</p>
  
  <pre><code>return array(  
   'controllers' =&gt; array(
          'invokables' =&gt; array(
              'Auth\Controller\User' =&gt; 'Auth\Controller\UserController',
          ),
      ), 
  
      'controller_plugins' =&gt; array(
        'invokables' =&gt; array(
            'AclPlugin' =&gt; 'Auth\Controller\Plugin\AclPlugin',
        ),  
      ),
  );
  </code></pre>