简体   繁体   English

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

[英]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. 我目前正在开发一个Joomla插件,我想在其中捕获事件onContentAfterSave ,以便将新保存的文章发布到URL缩短器中,以用于社交网络。

Can someone help me on how to calculate the appropriate SEF URL to an article detail view (no menu entry!)? 有人可以帮助我如何为文章详细信息视图计算适当的SEF URL(无菜单项!)?

The URL should be like: http://<domain>.<tld>/<category-id>-<category-title>/<article-id>-<article-title>.html 该URL应类似于: 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. 使用JRoute将非基于SEF参数的URL转换为SEF URL。

In the case of linking to a standard Joomla article, it would need the following information: 如果要链接到标准的Joomla文章,则需要以下信息:

$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. $ article-> id可通过onContentAfterSave获得,尽管您可能已经将$ article对象称为别的对象,在这种情况下,可以适当地重命名。

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. 如果确实有用于文章的菜单项,则添加菜单项的id的Itemid参数,它将加载与该菜单项相关的模块。

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 我可以通过以下帖子解决此问题: https : //joomla.stackexchange.com/questions/36/how-do-i-generate-a-sef-url-in-a-custom-module-instead-of -真实网址

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. 该解决方案的真正好处是,它还可以与子目录中包含的安装一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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