2014-11-04 29 views
-1

外我有一個類:​​,我需要的學說服務$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"; 
    } 

} 
+0

你在擴展任何類嗎?你的錯誤是什麼?什麼不起作用?計算機會爆發火焰嗎?> – Kisaragi 2014-11-04 18:33:42

+0

@Kisaragi我的類實現了另一個接口:'SaveHandlerInterface'。我的課:SessionDatabase類實現'SaveHandlerInterface。 我沒有一個PHP的錯誤,當我實現了'ServiceManagerAwareInterface',功能'getServiceLocator()'和'setServiceLocator(ServiceLocatorInterface $服務定位器)'不能運行 – 2014-11-04 18:41:52

回答

0

最簡單的方法是使用ServiceLocatorAwareTrait(ZF2的一部分)。如果你確實使用它來確保你的類實現了ServiceLocatorAwareInterface(你實際上並不需要實現它,因爲特徵爲你所用,但你必須將它添加到你的類的implements子句中)。

所以你得到的東西是這樣的:

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

class ClassA implements ServiceLocatorAwareInterface { 
    use ServiceLocatorAwareTrait; 
} 

然後,你可以調用$這個 - > getServiceLocator()得到的ServiceLocator實例的保持。

但是如果你只需要學說的EntityManager這是更好的做法,只是這種注入一個依賴,而不是注入的ServiceLocator。

+0

謝謝回答!我實現了'ServiceLocatorAwareInterface'和'使用ServiceLocatorAwareTrait'。但是當我嘗試'$ this-> getServiceLocator() - > get('ServiceName')'我得到一個錯誤。 PHP **致命錯誤:**調用一個成員函數的get()一個非對象... 我的類: '類SessionDatabase實現SaveHandlerInterface,ServiceLocatorAwareInterface { 使用ServiceLocatorAwareTrait;' – 2014-11-04 19:42:07

+0

你能顯示您的完整代碼並指出您遇到錯誤的哪一行?請在編輯部分將它放在原始問題的代碼塊中,以便閱讀。 – Ruben 2014-11-04 20:07:15

+2

另一個重要的事情是:如果你想自動注入服務定位器,你必須通過服務管理器獲得你的SessionDatabase類實例。 – jmleroux 2014-11-04 20:24:53

0

終於解決了這個問題。我的類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