【问题标题】:Joomla setRedirect doesn't workJoomla setRedirect 不起作用
【发布时间】:2012-10-20 02:15:23
【问题描述】:

我有一个简单的 Joomla 控制器,但我无法重定向任何东西。

根据文档:

class MyController extends MyBaseController {

 function import() {
    $link = JRoute::_('index.php?option=com_foo&ctrl=bar');
    $this->setRedirect($link);
  }

}
//The url contains & html escaped character instead of "&"

这应该可以,但是我得到了一个格式错误的 URL。我在这里缺少什么吗?为什么Joomla 将所有的“&”字符转换为&'s?我应该如何使用 setRedirect?

谢谢

【问题讨论】:

    标签: redirect joomla joomla2.5 joomla-extensions


    【解决方案1】:

    好的,我修好了。所以如果有人需要它:

    而不是

    $link = JRoute::_('index.php?option=com_foo&ctrl=bar');
    $this->setRedirect($link);
    

    使用

    $link = JRoute::_('index.php?option=com_foo&ctrl=bar',false);
    $this->setRedirect($link);
    

    让它工作。

    【讨论】:

      【解决方案2】:

      很高兴您找到了答案,顺便说一下,JRoute::_() 中的布尔参数默认为 true,对于 xml 合规性很有用。它的作用是在静态方法内部,它使用 htmlspecialchars php 函数,如下所示:$url = htmlspecialchars($url) 替换 & 用于 xml。

      【讨论】:

        【解决方案3】:

        试试这个。

        $mainframe = &JFactory::getApplication();
        $mainframe->redirect(JURI::root()."index.php?option=com_foo&ctrl=bar","your custom message[optional]","message type[optional- warning,error,information etc]");
        

        【讨论】:

        • 我同意你的回答@jobin cause $this->setRedirect($link); setRedirect() 未在您的自定义类中定义,因此请使用 jobin 回答它确实有效
        • 其实我有,只是我在 JController 和我自己的控制器之间放了一个控制器,这样我自己的控制器就可以共享某些功能。不过谢谢
        【解决方案4】:

        检查 Joomla 源代码后,您可以快速了解发生这种情况的原因:

        if (headers_sent())
            {
                echo "<script>document.location.href='" . htmlspecialchars($url) . "';</script>\n";
            }
            else
            {
            ... ... ...
        

        问题是您的页面可能已经输出了一些数据(通过回显或其他方式)。 在这种情况下,Joomla 被编程为使用简单的 javascript 重定向。但是,在此 javascript 重定向中,它已将 htmlspecialchars() 应用于 URL。

        一个简单的解决方案是不使用 Joomlas 函数,直接以更有意义的方式编写 javascript:

        echo "<script>document.location.href='" . $url . "';</script>\n";
        

        这对我有用:)

        【讨论】:

          【解决方案5】:

          /libraries/joomla/application/application.php

          找到第 400 行

              // If the headers have been sent, then we cannot send an additional location header
              // so we will output a javascript redirect statement.
              if (headers_sent())
              {
                  echo "<script>document.location.href='" . htmlspecialchars($url) . "';</script>\n";
              }
          

          替换为

              // If the headers have been sent, then we cannot send an additional location header
              // so we will output a javascript redirect statement.
              if (headers_sent())
              {
                  echo "<script>document.location.href='" . $url . "';</script>\n";
              }
          

          这行得通!

          【讨论】:

          • 这是最糟糕的解决方案。切勿修改第 3 方库来修复不是错误的内容。除非您愿意向核心 Joomla 人员发出拉取请求以合并您的更改,否则您不应该进行更改。事实上,您可能会严重破坏应用程序的其余部分,并从根本上改变其他 3rd 方库可能依赖的 Joomla 行为。
          猜你喜欢
          • 1970-01-01
          • 2023-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-23
          相关资源
          最近更新 更多