gpt4 book ai didi

error-handling - 如何在Zend Framework 2中将变量传递到错误 View 脚本?

转载 作者:行者123 更新时间:2023-12-03 07:59:05 25 4
gpt4 key购买 nike

我捕获了特定于框架的异常,如下所示:

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');
});
}

然后,当我尝试访问 $foo中的 /module/Application/view/error/index.phtml时,我得到了 Notice: Undefined variable: foo

如何将变量传递到错误 View 脚本并在那里访问它?

最佳答案

从onBootstrap内部,它将变量传递到布局。

尝试这个:

$this->layout()->foo

关于error-handling - 如何在Zend Framework 2中将变量传递到错误 View 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37207411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com