15

Zend が提供する Skeleton Application が を処理しないことに気付きましたerror 500。ZF1にはそれを処理する があったことを私は知ってErrorControllerいます。私はオンラインでいくつかの調査を行いましたが、これに対する明確な解決策は見つかりませんでした。

では、ZF2 でのエラー処理の最良の方法は何ですか。モジュールごとに、またはグローバルな例外/エラーハンドラーに基づいていますか?

ini_set('display_errors', true);に追加する別の解決策があることは知ってindex.phpいますが、その解決策はあまり好きではありません。フレームワークは、エラーを処理するための何らかの方法を提供する必要があるようです。

4

1 に答える 1

31

例外をグローバルにキャッチしている次の例のように、例外をキャッチした後は、必要に応じて例外を処理できます...:

あなたの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...
}
于 2013-06-04T04:19:29.527 に答える