मैं प्लगइन क्लास में सेवा लोकेटर/इकाई प्रबंधक प्राप्त करने का प्रयास कर रहा हूं, मैं इसे कैसे प्राप्त कर सकता हूं।नियंत्रक प्लगइन में 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',
),
),
);
प्लगइन वर्ग के अंतर्गत नियंत्रक \ प्लगइन \ है और बदले सरणी (का उपयोग करके invokable है 'controller_plugins' => सरणी ( 'invokables' => सरणी ( 'एक्लप्लगिन' => 'एथ \ कंट्रोलर \ प्लगइन \ एक्लप्लगिन', ), ),); लेकिन किसी भी तरह मैं त्रुटि जब मैं $ इस- का उपयोग हो रही है> getController() -> getServiceLocator() -> मिल ('doctrine.entitymanager.orm_default'); प्लगइन में वर्ग त्रुटि गंभीर त्रुटि है: /module/Auth/src/Auth/Controller/Plugin/AclPlugin.php – Developer
में एक गैर-वस्तु पर एक सदस्य समारोह getServiceLocator() करने के लिए कॉल आप कृपया अपने नियंत्रक प्लगइन वर्ग पेस्ट कर सकते हैं? मैं यह वही करता हूं और यह मेरे लिए काम करता है। साथ ही, कृपया अपना ZF2 संस्करण (बीटाएन/आरसीएन/2.0.0) बताएं। –
संस्करण zf2 स्थिर 2.0.0 रिलेज़ है। कृपया – Developer