2

I'm trying to figure out why I can't get a correct absolute SEF url with JRoute.

I tried with this:

JRoute::_(JUri::root().'index.php?Itemid=164')

but it won't correctly SEF render it, it will just print like this

http://mysite.com/index.php?Itemid=164` 

If I remove the JUri::root() part it will render correctly, but as a relative url ofc.

Any suggestion? What am I doing wrong? Am I misunderstanding from the docs

_(string $url, boolean $xhtml = true, integer $ssl = null) : string

Arguments

$url: Absolute or Relative URI to Joomla resource.

1
  • dirname seems to work >dirname(JUri::root()) . JRoute::_('index.php?option=com_test&task=mycontroller.mytask');
    – Lobos
    Commented Apr 17, 2018 at 15:43

4 Answers 4

2

You could try this way:

$itemid = 164;
$application = JFactory::getApplication();
$menu = $application->getMenu();
$item = $menu->getItem( $itemid );
$link = new JURI( $item->link );
$link->setVar( 'ItemId', $itemid );

echo JRoute::_( $quoteLink, true, -1 ); ?>


From JRoute documentation:

Routing URLs**

The JRoute::_ method translates the internal Joomla! URL to a custom URL. JRoute::_ has three parameters and its prototype is:

JRoute::_($url, $xhtml = true, $ssl = null);

Where:

$url is a string containing the absolute or relative internal Joomla! URL.

$xhtml is a boolean value that specifies whether or not the output should be in XHTML. This parameter is optional and if omitted defaults to true.

$ssl is an integer value that specifies whether the URI should be secure. It should be set to 1 to force the URI to be secure using the global secure site URI, 0 to leave it in the same state as when it was passed, and -1 to force the URI to be unsecure using the global unsecure site URI.

It's not obvious from this documentation, but using **$ssl = -1** will force an insecure URI, something like http://joomlasite.com

Update for your comment:

Another option could be the following:

echo JUri::root() . trim( JRoute::_( $quoteLink ), '/' );

A little «hacky», but it does the work... ;)

5
  • Thank you! I should have mentioned tho that I'm trying also to be SSL "agnostic", but thinking about it again I guess I have no choices in this regard: either set up an SSL in my dev environment too, and work with SSL only absolute paths, or leave all resources as relative paths and let system handle it. Any thoughts on this?
    – Gruber
    Commented Apr 20, 2016 at 14:28
  • @Gruber... then maybe you could do what I suggest in the Update part of my answer.
    – FFrewin
    Commented Apr 20, 2016 at 14:44
  • Thank you looks sensed, a bit hacky but effective. Seems weird there is no proper way to achieve this!
    – Gruber
    Commented Apr 20, 2016 at 23:32
  • @Gruber, sort of weird indeed.
    – FFrewin
    Commented Apr 21, 2016 at 19:33
  • I never used *$ssl = -1; I only use 0, 1 or 2; see in sources * @param integer $ssl Secure state for the resolved URI. * 0: (default) No change, use the protocol currently used in the request * 1: Make URI secure using global secure site URI. * 2: Make URI unsecure using the global unsecure site URI. Commented Feb 3, 2017 at 20:07
1

You can also use proper way to get full url as:

$config = JFactory::getConfig();
$mode = $config->get('force_ssl', 0) == 2 ? 1 : (-1);
echo JRoute::_('index.php?Itemid=164',false,$mode);
1
  • That doesn't make sense to set the mode to -1; use 2 for make unsecure site URI. Just set the mode: $mode = ($config->get('force_ssl', 0) == 2) ? 1 : 2; Commented Feb 3, 2017 at 20:14
1

You can not achieve it the way you trying to because if you look at the router code:

link that starts with index.php should be passed, not with http(s)

The correct way to achieve routing is:

JRoute::_('index.php?Itemid=164')

If you would like to have absolute sef url you need to prepend root after:

JUri::root().JRoute::_('index.php?Itemid=164')
1

UPDATE 2021

It seems that somewhere around version 3.9.7, they included a fourth parameter in this function, so now the function definition is:

public static function _($url, $xhtml = true, $tls = self::TLS_IGNORE, $absolute = false)

So you can call it like this:

JRoute::_('index.php?Itemid=164', true, 0, true);

The fourth argument forces the URL to be absolute.

This is probably also backwards compatible, because PHP silently disregards additional arguments for user-defined functions. However, you'll still have to solve it somehow for versions earlier than 3.9.7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.