2014-08-28 18 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界面定义的方法缺失。