外我有一個類:,我需要的學說服務$this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
。getServiceLocator()控制器
我試着實現接口ServiceManagerAwareInterface
,但函數getServiceLocator()
和setServiceLocator(ServiceLocatorInterface $serviceLocator)
不起作用。
有人使用ZF2控制器類的外部服務定位?這是可能的?
<?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 2014-11-04 18:33:42
@Kisaragi我的類實現了另一個接口:'SaveHandlerInterface'。我的課:SessionDatabase類實現'SaveHandlerInterface。 我沒有一個PHP的錯誤,當我實現了'ServiceManagerAwareInterface',功能'getServiceLocator()'和'setServiceLocator(ServiceLocatorInterface $服務定位器)'不能運行 – 2014-11-04 18:41:52