例外をグローバルにキャッチしている次の例のように、例外をキャッチした後は、必要に応じて例外を処理できます...:
あなたのonBootstrap
メソッドではModule.php
、イベントが発生したときに実行する関数をアタッチできます。次は、エラー (例外) が発生したときに実行する関数をアタッチします。
public function onBootstrap(MvcEvent $e)
{
$application = $e->getApplication();
$em = $application->getEventManager();
//handle the dispatch error (exception)
$em->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'handleError'));
//handle the view render error (exception)
$em->attach(\Zend\Mvc\MvcEvent::EVENT_RENDER_ERROR, array($this, 'handleError'));
}
次に、任意の方法でエラーを処理する関数を定義します。以下に例を示します。
public function handleError(MvcEvent $e)
{
//get the exception
$exception = $e->getParam('exception');
//...handle the exception... maybe log it and redirect to another page,
//or send an email that an exception occurred...
}