user2737980
user2737980

Reputation: 233

How to get full url of a article by it's ID in joomla?

I have article id, How can I get valid full url of this article? This article already associated with menu but I might not know, is there any easy way in php to get url? I am using joomla 3.2 I tried following already.

$article = ControllerLegacy::getInstance('Content')->getModel('Article')->getItem($article‌​Id); 
JRoute::_(ContentHelperRoute::getArticleRoute($articleId,$article->catid))

Upvotes: 3

Views: 6246

Answers (3)

Hirdesh Vishwdewa
Hirdesh Vishwdewa

Reputation: 2362

I am writing this because I think this information is useful to all other users who want full current URL anywhere in Joomla not only in articles.

In Joomla use JURIclass to get URLs. Functions like root(), current() & base() will be used according to the need.

echo 'Joomla root URI is ' . JURI::root();
output:-Joomla root URI is http://localhost/joomla/

echo 'Joomla current URI is ' . JURI::current();
output:-Joomla current URI is http://localhost/joomla3/index.php/somealias

Note:- current() will give the whole URI except the query string part, for example,

IF your full URL is http://localhost/joomla3/index.php/somealias?id=1 then current() will only return this-> http://localhost/joomla3/index.php/somealias

While, if you use JURI::getInstance()->toString() then it will return this->

http://localhost/joomla3/index.php/somealias?id=1

For more information see these links->

  1. https://docs.joomla.org/JURI/root
  2. https://docs.joomla.org/JURI/current
  3. https://docs.joomla.org/JURI/getInstance

Upvotes: 2

Prakash Thapa
Prakash Thapa

Reputation: 1285

You can use like this

 $article = JControllerLegacy::getInstance('Content')
            ->getModel('Article')->getItem($articleId);

  $url =  JRoute::_(ContentHelperRoute::getArticleRoute($articleId, 
                      $article->catid, 
                      $article->language))

Upvotes: 7

felipsmartins
felipsmartins

Reputation: 13549

Maybe the JURI (from Joomla! API) help you:

exemple:

echo 'Joomla current URI is ' . JURI::current() . "\n";

might output

Joomla base URI is http://localhost/joomla/

Upvotes: 0

Related Questions