我已经做了一些自定义异常监听器,因此在表中的数据库中,我得到了旧的 url,现在是 404 错误,以及用户在到达旧 url 时应重定向到的另一个 url。问题是,在 DEV 环境中一切正常,但在 PROD 环境中运行时遇到问题(它抛出 503 服务不可用错误)。
有人知道可能出了什么问题吗?
服务.yml:
services:
coupons.exception.action_listener:
class: Coupons\WebBundle\EventListener\ExceptionListener
arguments: [@service_container, @templating]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
异常监听器:
namespace Coupons\WebBundle\EventListener;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\TwigBundle\TwigEngine;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
class ExceptionListener
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var TwigEngine
*/
protected $templating;
/**
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container, TwigEngine $templating){
// assign value(s)
$this->container = $container;
$this->templating = $templating;
}
/**
*
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
// get exception
$exception = $event->getException();
// get path
$path = $event->getRequest()->getPathInfo();
$url = $event->getRequest()->getUri();
$repository = $this->container->get('doctrine')->getRepository('CouponsWebBundle:SeoNotFound');
$seonotfound = $repository->createQueryBuilder('s')
->where('s.urlFrom LIKE :url')
->andWhere('s.status = 1')
->setParameter('url', $url.'%')
->getQuery()
->getOneOrNullResult();
if($seonotfound != NULL){
$event->setResponse(new RedirectResponse($seonotfound->getUrlTo(),$seonotfound->getRedirectType()));
}
}
}
最佳答案
可能您在异常监听器中遇到了另一个异常,请检查 symfony 的日志文件。
关于Symfony2 : Custom Exception Listener not working on production env,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21631445/