【发布时间】: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