1

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.

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

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

  • 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 Apr 20 '16 at 14:28
  • @Gruber... then maybe you could do what I suggest in the Update part of my answer. – FFrewin Apr 20 '16 at 14:44
  • Thank you looks sensed, a bit hacky but effective. Seems weird there is no proper way to achieve this! – Gruber Apr 20 '16 at 23:32
  • @Gruber, sort of weird indeed. – FFrewin Apr 21 '16 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. – Tom Kuschel Feb 3 '17 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);
  • 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; – Tom Kuschel Feb 3 '17 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')

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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