Using JRoute::_() in Joomla administrator

udjamaflip picture udjamaflip · Jul 19, 2011 · Viewed 11.9k times · Source

I have a custom component I'm working on and I'm writing an import script which is running in the administration area. I have the following code:

$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
$newUrl = JRoute::_($newUrl);

the first part works returning similiar to this:

index.php?option=com_content&view=article&id=45:joomla-sociable-and-sharethis-module&catid=18

the second part shows it like this:

/administrator/index.php?option=com_content&view=article&id=45:joomla-sociable-and-sharethis-module&catid=18

Both of the above urls are as you'd expect the component com_content to render these urls as if I wanted to use them within the administration area.

Any idea how to force JRoute to work as it would when used in the frontend?

NB: This is being used within a controller of my component, if it makes any difference and I'm including require_once (JPATH_SITE . '/components/com_content/helpers/route.php');

Answer

udjamaflip picture udjamaflip · Jul 20, 2011

For those who find this on Google and struggle with using JRoute::_() and contentHelper::getArticleRoute().

$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);

// better will be check if SEF option is enable!
$router = new JRouterSite(array('mode'=>JROUTER_MODE_SEF));
$newUrl = $router->build($newUrl)->toString(array('path', 'query', 'fragment'));
// SEF URL !
$newUrl = str_replace('/administrator/', '', $newUrl);
//and now the tidying, as Joomlas JRoute makes a cockup of the urls.
$newUrl = str_replace('component/content/article/', '', $newUrl);