Symfony2 : Custom Exception Listener not working on production env

标签 symfony exception event-listener

我已经做了一些自定义异常监听器,因此在表中的数据库中,我得到了旧的 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/

相关文章:

laravel - $this->table() 抛出 [InvalidArgumentException] 行必须是数组或 TableSeparator 实例

java - org.hibernate.PropertyAccessException : exception setting property value with CGLIB

java - 获取 java.rmi.UnmarshalException : unrecognized method hash: method not supported by remote object

forms - 如何根据用户选择的字段值修改 Symfony2 表单?

java - 如何在监听器内迭代 JPanel 的子组件

symfony - 覆盖 FOSUserBundle 路由 Symfony2

php - JMSSerializerBundle,反序列化后持久化没有关系

mysql - 在 OrderBy Doctrine Querybuilder 中使用 IS NULL 和 COALESCE

c++ - 使用 new 分配多维数组

javascript - 如何使用事件监听器或设置事件来隐藏和显示单击子菜单