Joomla setRedirect doesn't work

こ雲淡風輕ζ 提交于 2019-12-03 22:13:59

Alright, I fixed it. So if anyone needs it:

instead of

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

use

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

to make it work.

Glad you found your answer, and by the way, the boolean parameter in JRoute::_() is by default true, and useful for xml compliance. What it does is that inside the static method, it uses the htmlspecialchars php function like this: $url = htmlspecialchars($url) to replace the & for xml.

Try this.

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

After inspecting the Joomla source you can quickly see why this is happening:

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

The problem is that your page has probably already output some data (via echo or some other means). In this situation, Joomla is programmed to use a simple javascript redirect. However, in this javascript redirect it has htmlspecialchars() applied to the URL.

A simple solution is to just not use Joomlas function and directly write the javascript in a way that makes more sense:

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

This works for me :)

/libraries/joomla/application/application.php

Find line 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";
    }

replace to

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

This works!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!