0
votes

To customize the error page I created an error404.html.twig page to handle the page not found exception like when the user enter an invalid url for example. it works fine but I also need to display different messages according to errors in my application.

For that I use the createNotFoundException() method with a message as parameter but the message doesnt appear when the exception is thrown I always have the message in the error404.html.twig template. So how can I display the message parameter from the createNotFoundException() in my error page template? Thanks for your help

1

1 Answers

0
votes

The error404.html.twig page is a catch around a generic NotFoundException: in fact you do see the debug trace in dev mode, but you see the fancy custom HTML in prod.

If you need custom 404 behavior you have to build your Response manually, injecting a proper 404 Not Found status, then a proper body.

$product = $em -> getRepository("Acme:Product") -> findOneById( $id );

if($product === NULL)
{

    $response = $this -> render('YourBundle::Errors/404.html.twig', array(
        'message'   => 'blah blah'
    ));

    $response -> setStatus(404);    

    return $response;

}

Or you could write a Listener waiting for such event, it's up to you.