我捕获特定于框架的异常,如下所示:
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('ZendMvcApplication', 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
。
如何将变量传递到错误视图脚本并在那里访问它
在onBootstrap中,它将变量传递给布局。
试试这个:
$this->layout()->foo