如何将变量传递到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('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

相关内容

  • 没有找到相关文章

最新更新