2

I would like to create a link in Administrator Panel that redirect a user to an SEF url in Frontend.

Code in Backend

<?php 
$link = JUri::root() . 'index.php?option=com_content&view=article&id=1';
echo '<a href="'. JRoute::_($link) . '">Test</a>'
?>

This can't be translated to SEF because I use JUri::root() in my $link.

If it's possible, what is the best way to do this?

1
  • Welcome to JSE, please take the tour ...and have a good look around the place to see if you can share you knowledge with others seeking support. Oct 14, 2019 at 8:01

1 Answer 1

6

Since 3.9 there's Joomla\CMS\Router\Route::link() method which takes client (site or administrator) as first argument.

$link = 'index.php?option=com_content&view=article&id=1';
echo '<a href="' . JRoute::link('site', $link) . '">Test</a>';

If you have the article object available, you should use ContentHelperRoute::getArticleRoute() and pass the slug instead of ID to remove extra database query and also category and language to ensure correct routing.

// Register helper class.
JLoader::register('ContentHelperRoute', JPATH_SITE . '/components/com_content/helpers/route.php');

$link = ContentHelperRoute::getArticleRoute($article->id . ':' . $article->alias, $article->catid, $article->language);
echo '<a href="' . JRoute::link('site', $link) . '">Test</a>';

Your Answer

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

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