加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – ControllerPlugin类中的ZF2 getServiceLocator

发布时间:2020-12-13 18:04:58 所属栏目:PHP教程 来源:网络整理
导读:我想在插件类中获取服务定位器/实体管理器,我该怎么做呢. 在我的控制器中,我得到这样的. public function getEntityManager(){ if(null === $this-em){ $this-em = $this-getServiceLocator()-get('doctrine.entitymanager.orm_default'); } return $this-em
我想在插件类中获取服务定位器/实体管理器,我该怎么做呢.

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

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()行上收到错误.因为这在插件类中不可用.

我怎样才能这样做,以便我可以获取一些记录并在插件中插入少量数据库.

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

我用this plugin来创建我的插件

任何指南都会受到关注.

更新:

namespace AuthControllerPlugin;

use ZendMvcControllerPluginAbstractPlugin;
use ZendEventManagerEventInterface as Event;
use ZendAuthenticationAuthenticationService;

use DoctrineORMEntityManager;
use AuthEntityUser;
use ZendMvcMvcEvent;
class AclPlugin extends AbstractPlugin
{

    /*
    * @var DoctrineORMEntityManager
    */
    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 AuthAclAclRules(); 

        $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 AuthEntityUser u');
            $resultIdentities = $query->execute();

            var_dump($resultIdentities);
            exit();


            return;

        } else {
            $matches->setParam('controller','AuthControllerUser'); // 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(
            'AuthControllerUser' => 'AuthControllerUserController',),'controller_plugins' => array(
      'invokables' => array(
          'AclPlugin' => 'AuthControllerPluginAclPlugin',);
你对“插件类”有什么意思?如果你正在谈论abount控制器插件,你可以使用(从控制器插件的范围)访问它:$this-> getController() – > getServiceLocator() – > get(‘doctrine.entitymanager.orm_default’) ;.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读