1
我正在開發一個Zend Framework 2應用程序,並且有關於錯誤處理的奇怪行爲。我在Module.php中的代碼:ZF2應用程序 - 每次觸發調度錯誤事件
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_ROUTE, [$this, 'onPreRoute'], 100);
$eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'handleError']);
}
public function onPreRoute(MvcEvent $e)
{
$serviceManager = $e->getTarget()->getServiceManager();
$router = $serviceManager->get('router');
$router->setTranslator($serviceManager->get('translator'));
}
public function handleError(MvcEvent $e)
{
$error = $e->getParam('error');
file_put_contents('error.log', $error . PHP_EOL, FILE_APPEND);
switch($error) {
case 'error-router-no-match':
$router = $e->getRouter();
$url = $router->assemble([], ['name' => 'home']);
header('Location: ' . $url);
exit;
}
}
正如您所見,我正在翻譯路線。這工作正常。但是,在每個請求上也會觸發調度錯誤事件。每次都會創建error.log文件。但是隻有在路由不存在的情況下才會執行重定向。我認爲這取決於翻譯或是我在Module.php中的代碼不正確?