Symfony2:自定义异常监听器不在生产环境中工作

时间:2014-02-07 15:22:02

标签: symfony exception event-listener

我已经做了一些自定义异常监听器,因此在表中的数据库中我有旧的url,现在是404错误,另一个用户应该在到达旧URL时重定向的url。问题是在DEV环境中一切正常,但我有问题让它在PROD环境中运行(它丢弃了503 Service Unavailable Error)。

有谁可能知道什么是错的?

services.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 }

ExecptionListener:

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()));
        }

    }
}

1 个答案:

答案 0 :(得分:2)

可能你的异常监听器中有另一个异常,请检查symfony的日志文件。