【问题标题】:Can Symfony simply reload a page request?Symfony 可以简单地重新加载页面请求吗?
【发布时间】:2011-03-03 16:34:10
【问题描述】:

我有一个应用程序从另一个应用程序接收请求。它检测查询字符串上的值,根据缓存值检查该值,如果它们不匹配,则需要清除其缓存并重新加载页面(建立新缓存)。不幸的是,我找不到告诉 Symfony 以完全相同的格式(协议、URI 路径、查询字符串等)重定向到当前页面的方法。我错过了什么?这一切都发生在isFirstCall() 的过滤器中。

谢谢。

【问题讨论】:

    标签: php symfony1 symfony-1.4 php-5.2


    【解决方案1】:

    我们已经在过滤器中完成了这项工作。

    这有点老套,但这里有一个在过滤器中进行重定向的示例...您必须自己测试缓存...

    class invalidateCacheFilter extends sfFilter {   
    
      public function execute($filterChain)   {
    
        $redirect=true;
        if($redirect===true)
        {
          $request = $this->getContext()->getRequest();
          /**
           This is the hacky bit. I am pretty sure we can do this in the request object, 
           but we needed to get it done quickly and this is exactly what needed to happen.
          */
          header("location: ".$request->getUri());
          exit();
        }
        $filterChain->execute();   
      }
    }
    

    【讨论】:

    • 像你一样,我无法找到一种方法让请求对象直接执行此操作,同时仍在重定向上执行过滤器(这排除了在我的情况下使用 forward())。 getUri() 方法完全符合我的需要,因为它包含有关请求的所有相关信息。当我第一次看到它时,我担心它不会包含查询字符串。谢谢。
    【解决方案2】:

    如果你想重定向,你可以这样做:

    if (true===$redirect)
    {
        return $this->getContext()->getController()->redirect($request->getUri());
    }
    

    【讨论】:

    • 准确地说,在过滤器中你必须使用$this->getContext()->getController()->redirect();
    • @MichalTrojanowski,谢谢。 (我从我的 Controller 中粘贴了原始版本。)
    猜你喜欢
    • 2017-10-21
    • 2022-01-25
    • 2012-09-03
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    相关资源
    最近更新 更多