search

Home  >  Q&A  >  body text

How to define error page in symfony2!

When using doctrine2 and defining a unique company name, an error is prompted when submitting again. How to redefine this error page.

我想大声告诉你我想大声告诉你2833 days ago862

reply all(2)I'll reply

  • 迷茫

    迷茫2017-05-16 16:47:16

    Doctrine 2 is an independent third-party library that encapsulates PDO. When PDO encounters a uniqueness conflict, it throws a PDOException.

    Under the default configuration, the error page of the development environment catches this PDOException and outputs a Message.

    There are two ways to redefine the error page of Symfony 2:

    (1) Create an app/Resources/TwigBundle/views/Exception/error.html.twig and modify it according to your needs.

    There are statustext, statuscode and other data available in the template.

    (2) Or just use Event to listen for the kernel.exception event:

    use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
    use Symfony\Component\HttpFoundation\Response;
    
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();
    
        // 你已经获得了Exception:可以针对性地写逻辑
    
        $response = new Response();
        $event->setResponse($response); // 如果你在event里设置了response,这个response就会返回给用户
    
        // ...
    }
    

    http://symfony.com/doc/current/book/internals.html#kernel-kernel-exception

    reply
    0
  • 黄舟

    黄舟2017-05-16 16:47:16

    The template of Symfony's The Book has been written very clearly. It will first check whether there is such a template under app/Resources. If not, it will go to the Bundle. The most important thing is to clear the cache for it to work

    reply
    0
  • Cancelreply