如何在布局中使用Controller

时间:2013-06-22 23:30:30

标签: zend-framework2

您好我想知道如何在布局视图中获取控制器对象。 我有一个名为MyAbstractAction的类(它有一个getAuthService()方法),它扩展了AbstractAction类,我的所有控制器都扩展了MyAbstractAction ..

好吧,我想在布局中使用那个方法,但我不能,我试着去做 应用程序/模块/应用/ Module.php

class Module{
    public function onBootstrap(MvcEvent $e)
    {

            $e->getApplication()->getServiceManager()->get('translator');
            $eventManager   = $e->getApplication()->getEventManager();
            $viewModel = $e->getViewModel();
            $c= $e->getController();
            $authService = $this->getAuth($e);  
            if($authService){
                  $viewModel->setVariable('controller',$c);
            }
            if($this->getAuth($e)->hasIdentity()){
                  $viewModel->setTemplate('layout/inside');
            }else{
                  $viewModel->setTemplate('layout/outside');      
            }
       }
     //More methods

}

但它不起作用,有人可以说我怎么做?

这是我的MyAbstractActionController类,MyAbstractActionController方法只带来服务管理器的服务:

class MyAbstractActionController extends AbstractActionController 
{
    public $authService;
            public $sm;
            public $sessionStorage;
            public $dbAdapter;
            public function getAuthService()
    {
                error_log("Ejecutando:".__METHOD__);
        if(!$this->authService)
        {
                    $this->authService = $this->getServiceLocator()->get('Auth\AuthService');
        }
                  return $this->authService;
    }
    public function getDbAdapter()
       {
          if(!$this->dbAdapter)
        {
                    $this->dbAdapter = $this->getServiceLocator()->get("Zend\Db\Adapter\Adapter");
        }
                  return $this->dbAdapter;
      }


      public function getSessionStorage()
      {
            error_log('Ejecutando: ' . __METHOD__);
            if(!$this->sessionStorage)
            {
                  $this->sessionStorage = $this->getServiceLocator()->get('Auth\Storage\MySessionStorage');

            }
            return $this->sessionStorage;
      }
}

提前致谢:)

0 个答案:

没有答案