Joomla:从ID获取内容插件中的文章SEF URL


Joomla: get article SEF URL in Content Plugin from ID

我目前正在开发一个Joomla插件,我想在其中捕捉事件onContentAfterSave,以便将新保存的文章发布到URL shortener,以便在社交网络上使用。

有人能帮助我如何计算文章详细信息视图(无菜单项!)的适当SEF URL吗?

URL应该类似于:http://<domain>.<tld>/<category-id>-<category-title>/<article-id>-<article-title>.html

我读过这篇文章,但这并不能真正提供解决方案。

我用这篇文章解决了这个问题:https://joomla.stackexchange.com/questions/36/how-do-i-generate-a-sef-url-in-a-custom-module-instead-of-the-real-url

提供的解决方案是:

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

这个解决方案真正好的部分是,它还可以处理包含在子目录中的安装。

使用JRoute将非基于SEF参数的URL转换为基于SEF的URL。

在链接到标准Joomla文章的情况下,它需要以下信息:

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

$article->id可以通过onContentAfterSave获得,尽管您可能已经用其他方法调用了$article对象,在这种情况下,可以适当地重命名。

如果您确实有一个用于项目的菜单项,那么添加该菜单项id的Itemid参数,它将加载与该菜单项相关的模块。