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

坚强是说给别人听的谎言 提交于 2019-12-20 02:44:08

问题


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

回答1:


You can use like this

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

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



回答2:


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



回答3:


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/
  • JURI class


来源:https://stackoverflow.com/questions/23702983/how-to-get-full-url-of-a-article-by-its-id-in-joomla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!