2014-08-28 48 views
0

我無法訪問我的定製視圖助手中的服務定位器。這裏是我的代碼:在視圖助手中訪問服務定位器

<?php 
    namespace Sam\View\Helper; 

    use Zend\View\Helper\AbstractHelper; 
    use Zend\ServiceManager\ServiceLocatorAwareTrait; 
    use Zend\ServiceManager\ServiceLocatorAwareInterface; 

    class Authenticated extends AbstractHelper imeplements ServiceLocatorAwareInterface 
    { 
     protected $authservice; 

     public function __invoke() 
     { 
      if (!$this->getAuthService()->hasIdentity()){ 
       return false; 
      } 
      return true;   
     } 

     public function getAuthService() 
     { 
      if (! $this->authservice) { 
       $this->authservice = $this->getServiceLocator()->get('AuthService'); 
      } 
      return $this->authservice; 
     } 
    } 
+0

這是助手的完整代碼嗎?看起來你錯過了在課堂上使用ServiceLocatorAwareTrait行。 – 2014-08-28 15:47:45

+0

是的,解決了它。謝謝。 – jkushner 2014-08-28 15:51:29

+0

你應該做出答案。 – jkushner 2014-08-28 15:51:44

回答

1

按照意見,如果你想使用的服務定位特質,你需要在類use ServiceLocatorAwareTrait線。您目前擁有的應該會給您一個錯誤,因爲您正在實施的ServiceLocatorAware界面定義的方法缺失。