Joomla setRedirect不起作用

时间:2012-10-30 17:01:19

标签: redirect joomla joomla2.5 joomla-extensions

我有一个简单的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 "&"

这应该可行,但我收到格式错误的网址。这里有什么我想念的吗?为什么Joomla转换所有“&”字符变成&的?我怎么想使用setRedirect?

谢谢

5 个答案:

答案 0 :(得分:11)

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

而不是

$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);

让它发挥作用。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

试试这个。

$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]");

答案 3 :(得分:0)

在检查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";

这对我有用:)

答案 4 :(得分:-3)

/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";
    }

这有效!

相关问题