1

我赶上框架的具体例外如下:如何将变量传递给Zend Framework 2中的错误视图脚本?

public function onBootstrap(MvcEvent $e) 
{ 
    $eventManager = $e->getApplication()->getEventManager(); 
    $moduleRouteListener = new ModuleRouteListener(); 
    $moduleRouteListener->attach($eventManager); 
    ... 
    $sharedManager = $e->getApplication() 
     ->getEventManager() 
     ->getSharedManager(); 
    $sm = $e->getApplication()->getServiceManager(); 
    $sharedManager->attach('Zend\Mvc\Application', MvcEvent::EVENT_DISPATCH_ERROR, 
     function ($e) use($sm) { 
      if ($e->getParam('exception')) { 
       $sm->get('Logger') 
        ->crit($e->getParam('exception')); 
      } 
      // trying to pass a variable 
      $e->getViewModel()->setVariable('foo', 'bar'); 
     }); 
} 

当我再尝试在/module/Application/view/error/index.phtml访问$foo我得到一个Notice: Undefined variable: foo

如何将变量传递给错误视图脚本并在那里访问?

回答

0

从onBootstrap中它将变量传递给布局。

试试这个:

$this->layout()->foo 
+0

非常感谢您!有用。但是,是否也可以将一个变量传递给视图脚本的ViewModel?我在问,因为我看到了一些例子,它似乎起作用,例如在这些accespted回答http://stackoverflow.com/a/18776447/2019043,http://stackoverflow.com/a/20800544/2019043。 – automatix

+0

无法真正找到可以直接访问变量的部分。 – KiwiJuicer

+0

对不起,你是对的。不过,这样做有可能吗? – automatix

相关问题