自从几天以来,我一直在寻找一种方法来创建一个symfony 2.8人员化page404controller,但是我没有找到任何关于此的帮助。我已经能够得到的唯一信息是KnpUniversity: KnpUniversity example
但是由于这个例子并不完整,我完全不知道该怎么做。 我已经有我个人化的error404.html.twig。 如果你有这个控制器的任何基本模板,以及对它的一些解释,我将非常感激。
对于任何补充信息不要犹豫。 因为我是初学者,所以对于错误控制器的创建并不了解。创建个性化页面404控制器symfony 2.8
是的,我已经阅读this symfony documentation,但它不帮助我。
下面是一个URL的一个例子,我要检查: my.website.com/en/hotel/england/london/palacio
的EN/{酒店|别墅} /英格兰/伦敦/帕拉西奥是作为一个URL正确的,但在网页行的事存在,因此我所做的是显示一个按钮,我可以重定向到:
my.website.com/en/hotel/england/london/ 但在URl与fr或en以及酒店或别墅不相对应的情况下,只需显示一个按钮即可进入主页。
我的URl的组成来自数据库。所以我想检查每个部分到对应于我的url的最后3个元素的数据库表。前两个比较容易,如下图所示。
Error404Controller
<?php
namespace FrontendBundle\Controller;
class Error404Controller extends Controller
{
/*
* @Route(
* "/{_locale}/{_type}/{$countryName}/{$cityName}/{$etablishementName}",
* requirements={
* "_locale": "en|fr",
* "_type": "hotels|villas|restaurants"
* }
* )
*/
public function showExceptionAction($countryName,$cityName,$etablishementName)
{
$em = $this->getDoctrine()->getManager();
$events = $em->getRepository('FrontendBundle:Error404')
->getUpcomingEvents();
//$urlreçu = $request->getUri();
//$urlredirect =0;
/*
* 1)Pays
* 2)Ville
* 3)Etablissment
* Si le Pays existe vérifier la Ville sinon retourner un bouton de redirection sur la home
* Si la Ville existe vérifier le l'établissement sinon retourner boutons de redirection sur la home et la recherche le pays
* Si l'établissement existe retourner les bouton home et recherche avec pays,ville,etablisee sinon retourner bouton home/recherche avec 1,2
*
* */
$country = $this->getDoctrine()->getRepository(FrontendBundle::Error404)->findCountryCityByName($countryName);
$city = $this->getDoctrine()->getRepository(FrontendBundle::Error404)->findCountryCityByName($cityName);
$etablishement = $this->getDoctrine()->getRepository(FrontendBundle::Error404)->findEtablishementBySlug($etablishementName);
return $this->render('app/Resources/TwigBundle/views/Exception/error404.html.twig', array(
'redirection' => $events,
));
}
}
Error404库
<?php
namespace FrontendBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;
use SG\FrameworkBundle\Model\CrudEntityInterface;
class Error404Repository extends EntityRepository
{
/*
* Recherche dans sg_content_translation
*/
public function findCountryCityBySlug($slug)
{
$qb = $this->createQueryBuilder()
->select('world.published_slug')
->from('super_table','world')
->where('world.published_slug= ?1')
->setParameter(1,$slug);
return $qb->getQuery()->getOneOrNullResult();
}
public function findEtablishementBySlug($slug){
$qb = $this->createQueryBuilder()
->select('world.published_slug')
->from('my_table','world')
->where('world.published_slug= ?1')
->setParameter(1,$slug);
return $qb->getQuery()->getOneOrNullResult();
}
}
这里是我的config.yml,如果我的理解以及压倒一切的
twig:
exception_controller: FrontendBundle:Error404:showExceptionAction
这是内我的服务.yml
services:
# twig.controller.exception:
# class: %twig.controller.exception.class%
# arguments: ["@twig", "%kernel.debug%", "@doctrine"]
app.exception_controller:
class: FrontendBundle\Controller\Error404Controller
arguments: ['@twig','%kernel.debug%']
我无法取代默认的异常控制器,还是我错了地方。有人能给我一个关于这个的想法吗?
我得到: ** URI“/ en/villas/france/bernadette”的控制器“FrontendBundle:Error404:showExceptionAction”不可调用。 **并且我在Internet上找到的解决方案不适用于ExceptionController。
感谢您的帮助:)
你们是不是要触发你的行动了404? –
您还必须设置404响应代码:Response :: HTTP_NOT_FOUND – COil
我再次在此处写入:Symfony是一个真正体面的框架。不要使用404控制器,使用由路径调用的控制器:“/ en/villas/france/bernadette”并处理该控制器中的响应。这是一个MVC框架的常规用例,没有什么特别的。如果别墅存在:显示,如果没有重定向,如果不存在重定向,则404显示为不显示,并且显示路线正确。 Le controller porte la logique:il fait lesvérificationset rend une page en fonction,c'est sonrôlepas besoin d'un controller404. –