简体   繁体   中英

Joomla: get article SEF URL in Content Plugin from ID

I'm currently developing a Joomla plugin in which I want to catch the event onContentAfterSave , in order to post the newly saved article to a URL shortener, for use on social networks.

Can someone help me on how to calculate the appropriate SEF URL to an article detail view (no menu entry!)?

The URL should be like: http://<domain>.<tld>/<category-id>-<category-title>/<article-id>-<article-title>.html

I've read this post , but that doesn't really provide a solution.

Use JRoute to convert the non-SEF parameters-based URL to the SEF one.

In the case of linking to a standard Joomla article, it would need the following information:

$url = JRoute::_('index.php?option=com_content&view=article&id='.$article->id);

$article->id is available via onContentAfterSave, though you may have called the $article object something else, in which case rename as appropriate.

If you do have a menu item for articles, then add an Itemid parameter of the menu item's id, and it will load the modules related to that menu item.

I was able to solve this problem with this post: https://joomla.stackexchange.com/questions/36/how-do-i-generate-a-sef-url-in-a-custom-module-instead-of-the-real-url

The provided solution is:

$rootURL = rtrim(JURI::base(),'/');
$subpathURL = JURI::base(true);
if(!empty($subpathURL) && ($subpathURL != '/')) {
    $rootURL = substr($rootURL, 0, -1 * strlen($subpathURL));
}
$url = $rootURL.JRoute::_(ContentHelperRoute::getArticleRoute($article->id,  $article->catid));

The really good part of this solution is, that it also works with installations contained in a subdirectory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM