我是symfony的新手,我想在找不到路线时生成或创建自定义404页面。
这是我得到的错误:
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
重定向功能:
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);
}
我该怎么办?
答案 0 :(得分:1)
您获取的错误意味着$ redirect变量为null - 未找到具有此类源URL的实体。
您可以执行全局错误页面http://symfony.com/doc/current/cookbook/controller/error_pages.html,但您也可以通过检查是否找到实体来解决它,这在这种情况下非常重要,例如:
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);
}
答案 1 :(得分:0)
您需要创建一个文件app / Resources / TwigBundle / views / Exception / error.html.twig,您可以在其中放置自定义错误页面。
您可以在http://symfony.com/doc/current/cookbook/controller/error_pages.html找到更多信息。
亲切的问候, 彼得(http://piotrpasich.com)