bourax webmaster
bourax webmaster

Reputation: 733

create a custom 404 page

I am new in symfony and I want to generate or create a custom 404 page when a route is not found.

here is the error I get :

FatalErrorException: Error: Call to a member function getDestinationURL() on a non-object in /home/smiles/Downloads/prototype/redirect-app/src/GS/RedirectBundle/Controller/RedirectController.php line 247

the redirect function :

    public function redirectAction($source)
{        
    $em = $this->getDoctrine()->getManager();
    $repository = $em->getRepository('GSRedirectBundle:Redirect');
    $redirect = $repository->findOneBySourceURL($source);
    $destination_url = $redirect->getDestinationURL();
    return $this->redirect($destination_url);
}

what can I do ??

Upvotes: 0

Views: 972

Answers (2)

Piotr Pasich
Piotr Pasich

Reputation: 2639

You need to create a file app/Resources/TwigBundle/views/Exception/error.html.twig and there you can put the customized error page.

You can find more info at http://symfony.com/doc/current/cookbook/controller/error_pages.html .

Upvotes: 0

Milos Tomic
Milos Tomic

Reputation: 361

The error you're getting means that $redirect variable is null - no entity was found with such source url.

You could do global error page http://symfony.com/doc/current/cookbook/controller/error_pages.html but you could also solve it with checking if entity was found or not, which is really crucial in cases like this, for example:

public function redirectAction($source)
{        
    $em = $this->getDoctrine()->getManager();
    $repository = $em->getRepository('GSRedirectBundle:Redirect');
    $redirect = $repository->findOneBySourceURL($source);
    if (null == $redirect) {

        return $this->redirect('my_resource_not_found_route');
    }
    $destination_url = $redirect->getDestinationURL();

    return $this->redirect($destination_url);
}

Upvotes: 1

Related Questions