2012-09-14 17 views
7

Sto cercando di ottenere localizzatore di servizi/gestore di entità nella classe di plug-in, Come posso ottenerlo.ZF2 getServiceLocator nella classe ControllerPlugin

Nel mio controller lo sto ottenendo in questo modo.

public function getEntityManager() 
{ 
    if(null === $this->em){ 
     $this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); 
    } 
    return $this->em; 
} 

public function setEntityManager(EntityManager $em) 
{ 
    $this->em = $em; 
} 

ma nella classe plugin sto ricevendo errore su $ this-> getServiceLocator() linea. perché questo non è disponibile nella classe plugin.

Come posso fare lo stesso in modo che possa recuperare alcuni record e inserire pochi nel database nel plugin.

Ho un oggetto MvcEvent $ e nella mia classe plugin, posso utilizzarlo per ottenere il gestore di entità?

ho usato this plugin di creare il mio plug

Ogni guida sarà appriciated.

aggiornamento:

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->hasIdentity()) { 
      $storage = $auth->getStorage()->read();    
      if (!empty($storage->role)) 
       $role = strtolower ($storage->role);    
      else 
       $role = "guest";    
     } else { 
      $role = "guest";    
     } 
     $app = $e->getParam('application'); 
     $acl   = new \Auth\Acl\AclRules(); 

     $matches  = $e->getRouteMatch(); 
     $controller = $matches->getParam('controller'); 
     $action  = $matches->getParam('action', 'index'); 

     $resource = strtolower($controller); 
     $permission = strtolower($action); 

     if (!$acl->hasResource($resource)) { 
      throw new \Exception('Resource ' . $resource . ' not defined'); 
     } 

     if ($acl->isAllowed($role, $resource, $permission)) { 

      $query = $this->getEntityManager($e)->createQuery('SELECT u FROM Auth\Entity\User u'); 
      $resultIdentities = $query->execute(); 

      var_dump($resultIdentities); 
      exit(); 


      return; 

     } else { 
      $matches->setParam('controller', 'Auth\Controller\User'); // redirect 
      $matches->setParam('action', 'accessdenied');  

      return; 
     } 


    } 


    public function getEntityManager($e) { 

     var_dump($this->getController()); // returns null 
     exit(); 
     if (null === $this->em) { 
      $this->em = $this->getController()->getServiceLocator()->get('doctrine.entitymanager.orm_default'); 

     } 
     return $this->em; 
    } 

    public function setEntityManager(EntityManager $em) { 
     $this->em = $em; 
    } 

} 

sto chiamando sopra di classe in module.php

public function onBootstrap(Event $e) 
    { 

     $application = $e->getApplication();   
     $services = $application->getServiceManager();   

     $eventManager = $e->getApplication()->getEventManager(); 
     $eventManager->attach('dispatch', array($this, 'loadConfiguration'),101); 

    } 

public function loadConfiguration(MvcEvent $e) 
    { 

     $e->getApplication()->getServiceManager() 
        ->get('ControllerPluginManager')->get('AclPlugin') 
        ->checkAcl($e); //pass to the plugin...  

    } 

sto registrando questo plugin in module.config.php

return array( 
'controllers' => array(
     'invokables' => array(
      'Auth\Controller\User' => 'Auth\Controller\UserController', 
     ), 
    ), 

    'controller_plugins' => array(
     'invokables' => array(
      'AclPlugin' => 'Auth\Controller\Plugin\AclPlugin', 
    ), 
    ), 
); 

risposta

8

Cosa fare significa con "Plugin Class"? Nel caso si stia parlando di plug-in del controller di abount, è possibile accedervi utilizzando (dall'ambito del plug-in del controller): $this->getController()->getServiceLocator()->get('doctrine.entitymanager.orm_default');.

Per altre classi, di solito creo una factory che inietta automaticamente l'istanza di ServiceManager. Per esempio, nella classe Modulo:

public function getServiceConfig() 
{ 
    return array(
     'factories' => array(
      'myServiceClass' => function(ServiceManager $sm) { 
       $instance = new Class(); 
       $instance->setServiceManager($sm); 
       // Do some other configuration 

       return $instance; 
      }, 
     ), 
    ); 
} 

// access it using the ServiceManager where you need it 
$myService = $sm->get('myService'); 
+0

classe del plugin è sotto controllo \ plugin \ ed è richiamabile utilizzando serie di ritorno ('controller_plugins' => array ( 'invokables' => array ( 'AclPlugin' => 'Auth \ Controller \ Plugin \ AclPlugin', ), ),); ma in qualche modo sto ricevendo errore quando uso $ this-> getController() -> getServiceLocator() -> get ('doctrine.entitymanager.orm_default'); errore di classe del plugin è Errore irreversibile: chiama ad una funzione membro getServiceLocator() su un oggetto non in /module/Auth/src/Auth/Controller/Plugin/AclPlugin.php – Developer

+0

Puoi incollare la classe del plug-in del controller? Lo faccio esattamente allo stesso modo e funziona per me. Inoltre, si prega di indicare la versione ZF2 (betaN/RCn/2.0.0). La versione –

+0

è relf stabile 2.0 zf2. per favore vedi la classe in questione sopra – Developer

2

cambiato la classe AclPlugin di cui sopra come sotto

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; 

use Zend\ServiceManager\ServiceManagerAwareInterface; 
use Zend\ServiceManager\ServiceManager; 
class AclPlugin extends AbstractPlugin implements ServiceManagerAwareInterface 
{ 

    /* 
    * @var Doctrine\ORM\EntityManager 
    */ 
    protected $em; 

    protected $sm; 

    public function checkAcl($e) 
    { 

     $this->setServiceManager($e->getApplication()->getServiceManager()); 

     $auth = new AuthenticationService(); 
     if ($auth->hasIdentity()) { 
      $storage = $auth->getStorage()->read();    
      if (!empty($storage->role)) 
       $role = strtolower ($storage->role);    
      else 
       $role = "guest";    
     } else { 
      $role = "guest";    
     } 
     $app = $e->getParam('application'); 
     $acl   = new \Auth\Acl\AclRules(); 

     $matches  = $e->getRouteMatch(); 
     $controller = $matches->getParam('controller'); 
     $action  = $matches->getParam('action', 'index'); 

     $resource = strtolower($controller); 
     $permission = strtolower($action); 

     if (!$acl->hasResource($resource)) { 
      throw new \Exception('Resource ' . $resource . ' not defined'); 
     } 

     if ($acl->isAllowed($role, $resource, $permission)) { 

      $query = $this->getEntityManager($e)->createQuery('SELECT u FROM Auth\Entity\User u'); 
      $resultIdentities = $query->execute(); 

      var_dump($resultIdentities); 
      foreach ($resultIdentities as $r) 
       echo $r->username; 
      exit(); 


      return; 

     } else { 
      $matches->setParam('controller', 'Auth\Controller\User'); // redirect 
      $matches->setParam('action', 'accessdenied');  

      return; 
     } 

    } 



    public function getEntityManager() { 

     if (null === $this->em) { 
      $this->em = $this->sm->getServiceLocator()->get('doctrine.entitymanager.orm_default'); 

     } 
     return $this->em; 
    } 

    public function setEntityManager(EntityManager $em) { 
     $this->em = $em; 
    } 

    /** 
    * Retrieve service manager instance 
    * 
    * @return ServiceManager 
    */ 
    public function getServiceManager() 
    { 
     return $this->sm->getServiceLocator(); 
    } 

    /** 
    * Set service manager instance 
    * 
    * @param ServiceManager $locator 
    * @return void 
    */ 
    public function setServiceManager(ServiceManager $serviceManager) 
    { 
     $this->sm = $serviceManager; 
    } 

} 
1

realtà sempre ServiceManager nel plug-in di controllo è abbastanza facile!

Basta usare: $this->getController()->getServiceLocator()

Esempio:

namespace Application\Controller\Plugin; 

use Zend\Mvc\Controller\Plugin\AbstractPlugin; 

class Translate extends AbstractPlugin 
{ 

    public function __invoke($message, $textDomain = 'default', $locale = null) 
    { 
     /** @var \Zend\I18n\Translator\Translator $translator */ 
     $translator = $this->getController()->getServiceLocator()->get('translator'); 

     return $translator->translate($message, $textDomain, $locale); 
    } 
}