2016-09-27 95 views

回答

0

當然。

services.yml,添加一個監聽器例外:

listener.exception: 
    class: Symfony\Component\HttpKernel\EventListener\ExceptionListener 
    arguments: ["MyWebsite\\Controller\\ExceptionController::exceptionAction"] 
    tags: 
     - {name: 'kernel.event_subscriber'} 

那麼你的控制器是一個普通的一個:

<?php 

namespace MyWebsite\Controller; 

use Symfony\Component\Debug\Exception\FlattenException; 
use Symfony\Component\HttpFoundation\Response; 

class ExceptionController extends AbstractController 
{ 
    public function exceptionAction(FlattenException $exception) 
    { 
     $text = $exception->getMessage() ; 
     if (isset(Response::$statusTexts[$exception->getStatusCode()])) { 
      $text = Response::$statusTexts[$exception->getStatusCode()]; 
     } 
     $response = new Response(null); 
     $response->setStatusCode($exception->getStatusCode()); 
     return $this->render(
      'exception.html.twig', // Here your template 
      ['error' =>$text], 
      $response 
     ); 
    } 
}