2

I have a simple Joomla controller, but I can't redirect anything.

According to the documentation:

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 "&"

This should work, but I get a malformed URL. Is there something I'm missing here? Why is Joomla converting all the "&" characters into &'s? How am I suppose to use setRedirect?

Thank you

drews
  • 146
  • 1
  • 8
Opi
  • 1,288
  • 1
  • 10
  • 14

5 Answers5

11

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.

Opi
  • 1,288
  • 1
  • 10
  • 14
1

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.

McRui
  • 1,879
  • 3
  • 20
  • 31
1

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]");
Jobin
  • 8,238
  • 1
  • 33
  • 52
  • i agree with your answer @jobin cause $this->setRedirect($link); setRedirect() not defined in your custom class so use jobin answer it's really works – Rakesh Sharma Oct 31 '12 at 09:41
  • actually I do have it, its just I put a controller between JController and my own so my own controllers can share certain functions. But thanks – Opi Oct 31 '12 at 19:43
0

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 :)

3rdLion
  • 159
  • 2
-3

/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!

  • 1
    This is the worst possible solution. Never modify a 3rd party library to fix something that **is not a bug**. Unless you feel comfortable issuing a pull request for the core Joomla people to merge your changes, you should not make the changes. As it is, you're probably breaking the *rest* of your application horribly, and fundamentally altering behaviors of Joomla that other 3rd party libraries may depend on. – user229044 Jun 19 '16 at 15:40