2012-09-14 10 views
7

मैं प्लगइन क्लास में सेवा लोकेटर/इकाई प्रबंधक प्राप्त करने का प्रयास कर रहा हूं, मैं इसे कैसे प्राप्त कर सकता हूं।नियंत्रक प्लगइन में ZF2 getServiceLocator

मेरे नियंत्रक में मुझे यह पसंद है।

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; 
} 

लेकिन प्लगइन कक्षा में मुझे $-> getServiceLocator() लाइन पर त्रुटि मिल रही है। क्योंकि यह प्लगइन वर्ग में उपलब्ध नहीं है।

मैं ऐसा कैसे कर सकता हूं ताकि मैं कुछ रिकॉर्ड प्राप्त कर सकूं और प्लगइन में डेटाबेस में कुछ डाल सकूं।

मेरे पास मेरी प्लगइन कक्षा में MvcEvent $ e ऑब्जेक्ट है, मैं इकाई प्रबंधक प्राप्त करने के लिए इसका उपयोग कर सकता हूं?

मैं this plugin का इस्तेमाल किया है मेरी प्लगइन

किसी भी गाइड appriciated किया जाएगा बनाने के लिए।

अद्यतन:

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; 
    } 

} 

मैं 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...  

    } 

मैं 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', 
    ), 
    ), 
); 

उत्तर

8

आप करते क्या "प्लगइन क्लास" के साथ मतलब है? यदि आप गणना नियंत्रक प्लगइन की बात कर रहे हैं, तो आप इसे नियंत्रित कर सकते हैं (नियंत्रक प्लगइन के दायरे से): $this->getController()->getServiceLocator()->get('doctrine.entitymanager.orm_default');

अन्य कक्षाओं के लिए, मैं आम तौर पर एक कारखाना बना देता हूं जो सेवा प्रबंधक उदाहरण को स्वचालित रूप से इंजेक्ट करता है। उदाहरण के लिए, मॉड्यूल कक्षा में:

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

प्लगइन वर्ग के अंतर्गत नियंत्रक \ प्लगइन \ है और बदले सरणी (का उपयोग करके invokable है 'controller_plugins' => सरणी ( 'invokables' => सरणी ( 'एक्लप्लगिन' => 'एथ \ कंट्रोलर \ प्लगइन \ एक्लप्लगिन', ), ),); लेकिन किसी भी तरह मैं त्रुटि जब मैं $ इस- का उपयोग हो रही है> getController() -> getServiceLocator() -> मिल ('doctrine.entitymanager.orm_default'); प्लगइन में वर्ग त्रुटि गंभीर त्रुटि है: /module/Auth/src/Auth/Controller/Plugin/AclPlugin.php – Developer

+0

में एक गैर-वस्तु पर एक सदस्य समारोह getServiceLocator() करने के लिए कॉल आप कृपया अपने नियंत्रक प्लगइन वर्ग पेस्ट कर सकते हैं? मैं यह वही करता हूं और यह मेरे लिए काम करता है। साथ ही, कृपया अपना ZF2 संस्करण (बीटाएन/आरसीएन/2.0.0) बताएं। –

+0

संस्करण zf2 स्थिर 2.0.0 रिलेज़ है। कृपया – Developer

2

रूप

नीचे
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

दरअसल नियंत्रक प्लगइन में ServiceManager हो रही काफी आसान है ऊपर AclPlugin वर्ग बदल गया है!

बस का उपयोग करें: $this->getController()->getServiceLocator()

उदाहरण:

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); 
    } 
} 
संबंधित मुद्दे