代码之家  ›  专栏  ›  技术社区  ›  Ricard Espinàs Llovet

请求uri太长symfony控制器

  •  1
  • Ricard Espinàs Llovet  · 技术社区  · 7 年前

    我正在尝试从我在项目中执行的所有查询中获取所有错误,并将这些错误重定向到一个名为“error”的控制器,该控制器将按我的意愿处理这些错误。当我重定向所有信息时,问题看起来像是通过get函数生成的url。

    我想如果这些信息是通过post发送的,那么这个问题就会消失,但是我显然没有在控制器中使用任何表单。所以,我怎么能对重定向函数说这些信息不应该和url一起,而应该通过post?

    可能是我想做的吗?

    内部控制器:

        try {
            $results = $queries->aQuery();
        } catch (ErrorException $errorException) {
            return $this->redirect($errorException->redirectResponse);
        }
    

    在服务查询中:

    public function aQuery(){
    
        $query="SELECT * FROM blabla ...";
        try {
            $stmt = $this->DB->->prepararQuery($query);
            $stmt->execute();
            $results = $stmt->fetchAll();
        } catch (DBALException $DBALException) {
            $errorException = new ErrorException($this->router->generate('error',
                [
                    'errorQuery' => $query,
                    'errorData' => "0 => '".$data1."', 1 ....",
                    'errorOrigin' => 'a place',
                    'errorResponseText' => $DBALException->getMessage()
                ]
            ));
            throw $errorException;
        }
    }
    

    错误异常:

    class ErrorException extends \Exception
    {
        /**
         * @var \Symfony\Component\HttpFoundation\RedirectResponse
         */
        public $redirectResponse;
    
        /**
         * ErrorException constructor.
         * @param \Symfony\Component\HttpFoundation\RedirectResponse $redirectResponse
         */
        public function __construct(string $redirectResponse)
        {
            $this->redirectResponse = $redirectResponse;
        }
    
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Brucie Alpha    7 年前

    如果您要实现的是集中处理异常的方法,请查看 https://symfony.com/doc/4.0/event_dispatcher.html#creating-an-event-listener 并使用kernel.exception事件

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        if (! $event->getException() instanceof ErrorException) {
            return;
        }
    
        // handle your custom ErrorException
        $response = new Response();
        $response->setContent($event->getException()->getMessage());
    
        // sends the modified response object to the event
        $event->setResponse($response);
    }
    
    推荐文章