ControllerPlugin 类中的 ZF2 getServiceLocator [英] ZF2 getServiceLocator in ControllerPlugin class

查看:26
本文介绍了ControllerPlugin 类中的 ZF2 getServiceLocator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在插件类中获取服务定位器/实体管理器,我怎样才能得到它.

I am trying to get service locator/entity manager in plugin class, How can I get that.

在我的控制器中,我得到了这样的结果.

In my controller I am getting it like this.

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

但是在插件类中,我在 $this->getServiceLocator() 行上遇到错误.因为这在插件类中不可用.

but in plugin class I am getting error on $this->getServiceLocator() line. because this is not available in plugin class.

我该如何做才能获取一些记录并在插件的数据库中插入一些记录.

How can I do the same so that I can fetch some records and insert few in database in plugin.

我的插件类中有 MvcEvent $e 对象,我可以利用它来获取实体管理器吗?

I do have MvcEvent $e object in my plugin class, I can make use of this to get entity manager?

我使用过 这个插件来创建我的插件

I have used this plugin to create my plugin

任何指南都将被应用.

更新:

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 中调用上面的类

I am calling above class 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...      

    }

我正在 module.config.php 中注册这个插件

I am registering this 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',
      ),  
    ),
);

推荐答案

插件类"是什么意思?如果您正在谈论控制器插件,您可以使用(从控制器插件的范围)访问它: $this->getController()->getServiceLocator()->get('doctrine.entitymanager.orm_default');.

What do you mean with "Plugin Class"? In case you're talking abount controller plugins, you can access it using (from the controller plugin's scope): $this->getController()->getServiceLocator()->get('doctrine.entitymanager.orm_default');.

对于其他类,我通常会创建一个自动注入 ServiceManager 实例的工厂.例如,在 Module 类中:

For other classes, I usually create a factory that injects the ServiceManager instance automatically. For example, in the Module class:

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');

这篇关于ControllerPlugin 类中的 ZF2 getServiceLocator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆