1

I'm accessing the Joomla framework from some external code as in this answer. The website is at http://localhost/joomla/ and the external code is in http://localhost/joomla/external/index.php

I have JPATH_BASE set properly to the Joomla root, and generate a URL like this:

JRoute::_('index.php?option=com_users&view=login')

But that comes back with the URL /joomla/external/index.php/login instead of what it should be: /joomla/index.php/login

How to fix this?

Edit: to make clear, I'm looking for a general solution for any folder structure. The external folder could be in another location e.g. http://localhost/external/

Community
  • 1
  • 1
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290

2 Answers2

2

Finally figured out a solution - I needed to set the site's base URL in the configuration.php file:

public $live_site = 'http://localhost/joomla/';

Now calling JRoute::_ returns the correct paths, wherever the external code is coming from.

DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
0

What you can do is, trim the trailing string from the end of the base URL and use that in JRoute, like so:

$base = rtrim(JUri::base(), JUri::base(true));

JRoute::_($base . '/index.php?option=com_users&view=login');

Testing for visual purposes:

$base     = JUri::base();
$baseTrue = JUri::base(true);
$baseRoot = rtrim($base, $baseTrue);

var_dump($base);
var_dump($baseTrue);
var_dump($baseRoot);
var_dump(JRoute::_($baseRoot . '/index.php?option=com_users&view=login'));

Output:

> 'http://example.loc/media/'                                (length=24)
> '/media'                                                   (length=6)
> 'http://example.loc'                                       (length=17)
> 'http://example.loc/index.php?option=com_users&view=login' (length=55)

Note: /media is just the directory I put my standalone PHP file in

Lodder
  • 19,758
  • 10
  • 59
  • 100
  • This doesn't work - I get `http://localhost/index.php...` when it should be `http://localhost/joomla/index.php...` BTW I should add that I'm looking for something that works in any circumstance like if the `external/` directory is in a completely different location. – DisgruntledGoat Apr 12 '16 at 15:11