7

In my module, this code is being used to generate the URL of the page:

JURI::base() . ContentHelperRoute::getArticleRoute(  $article->id,  $article->catid )

This works exaclty as it should and produces a URL that looks like:

http://example.com/index.php?option=com_content&view=article&id=83&catid=26&Itemid=162

But I would like to know if there is a way to get the module to generate the SEF URL for the current page. Something like:

http://example.com/resources/blog/83-illustration

3 Answers 3

14

Use JRoute for this:

rtrim(JUri::base(), '/') . JRoute::_(ContentHelperRoute::getArticleRoute(  $article->id,  $article->catid ))

but you don't need JUri::base() if you don't want to link to a different domain.

4
  • 1
    This worked and I will make it as "answered" but I was wondering why the new, SEF URL has two slashes after the domain name bite-sized-joomla.com//our-work/illustration-photography/…
    – rleathers
    Commented Apr 22, 2014 at 18:44
  • 3
    the reason is because JUri::base() returns a full uri with "/" at the end. and JRoute returns a absolute uri with a "/" at the beginning. I will edit the answer. Commented Apr 22, 2014 at 18:46
  • 1
    I believe that should be rtrim(JUri::base(), '/')... instead lf ltrim(JUri::base(), '/')... as the extra slash is at the end of the JUri::base() output. Commented Apr 3, 2016 at 14:46
  • I think you are right I updated the answer, thx. Commented Apr 3, 2016 at 15:09
4

There is a system plugin called System: SEF. When enabled, this plugin scans the final HTML output just before it is sent to the browser, and replaces any non-SEF links with their SEF counterparts by running it through JRoute::_($url);. This only works for internal links though beginning with index.php, so it won't work if the link starts with http://.

Try removing JUri::base() (and thus removing the http:// from the link) and it should work if the plugin is enabled.

3

Here is another way of getting the correct root URL of the site. This also takes care if your site is in subfolder

$rootURL = rtrim(JURI::base(),'/');
        $subpathURL = JURI::base(true);
        if(!empty($subpathURL) && ($subpathURL != '/')) {
            $rootURL = substr($rootURL, 0, -1 * strlen($subpathURL));
        }

Then you can do

$url = $rootURL.JRoute::_(ContentHelperRoute::getArticleRoute(  $article->id,  $article->catid ));

Your Answer

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

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