【问题标题】:accessing service locator in view helper在视图助手中访问服务定位器
【发布时间】:2014-08-28 15:41:41
【问题描述】:

我无法在我的自定义视图助手中访问服务定位器。这是我的代码:

    <?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;
        }
    }

【问题讨论】:

  • 这是帮助程序的完整代码吗?看来您在课堂上缺少 use ServiceLocatorAwareTrait 行。
  • 是的,解决了它。谢谢。
  • 你应该这样回答。

标签: zend-framework2 view-helpers service-locator


【解决方案1】:

根据 cmets,如果您想使用服务定位器 trait,您需要在类中添加 use ServiceLocatorAwareTrait 行。您当前拥有的内容应该会给您一个错误,因为您正在实现的 ServiceLocatorAware 接口定义的方法丢失了。

【讨论】:

    猜你喜欢
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多