Using JRoute::_() in Joomla administrator
Asked Answered
Z

5

5

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

Zebada answered 19/7, 2011 at 11:42 Comment(0)
Z
7

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);
Zebada answered 20/7, 2011 at 15:50 Comment(1)
how can i get article url in cli?Postrider
W
6

Here's a snippet that will work for Joomla 3.6

$routerOptions = [];
if (JFactory::getConfig()->get('sef')) {
    $routerOptions['mode'] = JROUTER_MODE_SEF;
}
$siteRouter = JRouter::getInstance('site', $routerOptions);
$link = $siteRouter->build($yourRoute)->toString();
$link = preg_replace('#^/administrator#', '', $link);
Whereunto answered 16/9, 2016 at 15:4 Comment(0)
O
2

In Joomla 3.9 they extended the JRoute class (now called Route class) to include a link() static method which solves this problem.

use Joomla\CMS\Router\Route;
$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
$newUrl = Route::link("site", $newUrl);

Route::link() works just the same as Route::_() except that you can must provide the additional first parameter to specify the client you want the URL built for. See https://api.joomla.org/cms-3/classes/Joomla.CMS.Router.Route.html#method_link.

Occam answered 16/12, 2019 at 16:29 Comment(0)
R
1

A nicer solution would be to create a new router instance, so, the code will be something like this:

$app    = JApplication::getInstance('site');
$router = &$app->getRouter();    

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

$newUrl = $router->build($newUrl);
$parsed_url = $newUrl->toString();
$parsed_url = str_replace('/administrator', '', $parsed_url);

This way you will always obtain the right URL for the item, no matter if it is a joomla article, K2 article, etc...

** Notice that depending on the type of the item ( k2, joomla, etc) , $newUrl should be obtained with the consequent method.

Rainy answered 23/10, 2013 at 12:25 Comment(1)
how can i get article url in cli?Postrider
B
1

I think that this one would be an easier solution:

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

This will give you the same result as the other two previous answers but with less coding.

Hope this helps.

Busman answered 19/1, 2014 at 5:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.