【问题标题】:getServiceLocator() outside Controller控制器外的 getServiceLocator()
【发布时间】:2014-11-04 18:23:57
【问题描述】:

我有一堂课:ClassA(),我需要教义服务$this->getServiceLocator()->get('Doctrine\ORM\EntityManager');

我尝试实现接口ServiceManagerAwareInterface,但函数getServiceLocator()setServiceLocator(ServiceLocatorInterface $serviceLocator) 不起作用。

有人在 ZF2 的 Controller 类之外使用了 ServiceLocator?有可能吗?

<?php

namespace DbSession\Service;

use Zend\Session\SaveHandler\SaveHandlerInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;

/**
 * Description of SessionDB
 *
 * @author rab
 */
class SessionDB implements SaveHandlerInterface, ServiceLocatorAwareInterface {
    use ServiceLocatorAwareTrait;
    /**
     * Session Save Path
     *
     * @var string
     */
    protected $sessionSavePath;

    /**
     * Session Name
     *
     * @var string
     */
    protected $sessionName;

    /**
     * Lifetime
     * @var int
     */
    protected $lifetime;

    /**
     * Constructor
     *
     */
    public function __construct() {
    }

    /**
     * Open the session
     * 
     * @return bool
     */
    public function open($savePath, $name) {
        echo "open session";
    }

    /**
     * Close the session
     * 
     * @return bool
     */
    public function close() {
        echo "close session";
    }

    /**
     * Read the session
     * 
     * @param int session id
     * @return string string of the sessoin
     */
    public function read($id) {

        echo "read session";
    }

    /**
     * Write the session
     * 
     * @param int session id
     * @param string data of the session
     */
    public function write($id, $data) {
        $this->getServiceLocator()->get('');
        echo "write";
    }

    /**
     * Destoroy the session
     * 
     * @param int session id
     * @return bool
     */
    public function destroy($id) {
        echo "destroy session";
    }

    /**
     * Garbage Collector
     * 
     * @param int life time (sec.)
     * @return bool
     */
    public function gc($maxlifetime) {
        echo "gc session";
    }

}

【问题讨论】:

  • 你在扩展任何类吗?你有什么错误?什么不工作?电脑会起火吗?>
  • @Kisaragi 我的班级实现了另一个接口:SaveHandlerInterface。我的班级:班级SessionDatabase implements SaveHandlerInterface。我没有 PHP 错误,当我实现 ServiceManagerAwareInterface 时,函数 getServiceLocator()setServiceLocator(ServiceLocatorInterface $serviceLocator) 没有运行

标签: php doctrine-orm zend-framework2 service-locator


【解决方案1】:

最简单的方法是使用 ServiceLocatorAwareTrait(ZF2 的一部分)。如果您确实使用它,请确保您的类实现了 ServiceLocatorAwareInterface(您实际上不必像 trait 那样为您实现它,但您必须将它添加到您的类的 implements 子句中)。

所以你会得到这样的结果:

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;

class ClassA implements ServiceLocatorAwareInterface {
    use ServiceLocatorAwareTrait;
}

然后你可以调用 $this->getServiceLocator() 来获取一个服务定位器实例。

但是,如果您只需要 Doctrine entitymanager,最好只注入这一依赖项而不是注入 ServiceLocator。

【讨论】:

  • 感谢您的回答!我实现了ServiceLocatorAwareInterfaceuse ServiceLocatorAwareTrait。但是当我尝试$this-&gt;getServiceLocator()-&gt;get('ServiceName') 时出现错误。 PHP 致命错误: 在非对象上调用成员函数 get() ... 我的班级:class SessionDatabase implements SaveHandlerInterface, ServiceLocatorAwareInterface { use ServiceLocatorAwareTrait;
  • 你能展示你的完整代码并指出你在哪一行得到错误吗?请将其放在编辑部分的原始问题的代码块中,以便于阅读。
  • 另一件重要的事情:如果你想自动注入服务定位器,你必须通过服务管理器获取你的 SessionDatabase 类实例。
  • @jmleroux 和 Ruben,我用我的代码更新了问题...$this-&gt;getServiceLocator()-&gt;get('ServiceName'); 返回错误
  • 这是因为您在构造函数中调用它。除了在构造函数中,你可以在任何地方调用它。
【解决方案2】:

终于解决了问题。我的课程 SessionDatabase() 是正确的。我的实例错了。

实现类:

class SessionDatabase implements SaveHandlerInterface, ServiceLocatorAwareInterface {

    use ServiceLocatorAwareTrait;

    public function write($id, $data) {
        $em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
    }

}

实例:

private function initDbSession(MvcEvent $e) {

        $serviceManager = $e->getApplication()->getServiceManager();

        $saveHandler = new SessionDatabase();
        $saveHandler->setServiceLocator($serviceManager);
}

感谢所有帮助过的人!!! :D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2011-06-06
    相关资源
    最近更新 更多