简体   繁体   English

Joomla 2.5 - 根据文章ID生成URL

[英]Joomla 2.5 - Generate URL based on article id

I am trying to generate an URL for an article basead on Article ID. 我正在尝试为文章ID生成文章库的URL。

After runing this query 运行此查询后

SELECT a.sectionid,
CASE WHEN CHAR_LENGTH( a.alias )
THEN CONCAT_WS( ":", a.id, a.alias )
ELSE a.id
END AS slug,
CASE WHEN CHAR_LENGTH( cc.alias )
THEN CONCAT_WS( ":", cc.id, cc.alias )
ELSE cc.id
END AS catslug
FROM #__content AS a
INNER JOIN #__categories AS cc ON cc.id = a.catid
WHERE a.id = $articleID

I store my result in $data and generate the link this way: 我将结果存储在$data并以这种方式生成链接:

$link = JRoute::_(ContentHelperRoute::getArticleRoute($data[0]->slug, $data[0]->catslug, $data[0]->sectionid));

The problem is that the generated link its not correct when my article doesn't belong to any menu. 问题是,当我的文章不属于任何菜单时,生成的链接不正确。

I noticed that when my article is not associated with any menu, the API gets the active menu id and add to the generated link, the parameter "&Itemid=MyActiveMenuId" (this happens in the route.php file). 我注意到,当我的文章没有与任何菜单相关联时,API获取active menu id并添加到生成的链接,参数"&Itemid=MyActiveMenuId" (这发生在route.php文件中)。 But since the article doesn't exist in the active menu, the generated link will not work. 但由于活动菜单中不存在该文章,因此生成的链接将不起作用。

I know that if the API just ignores the "item menu id" instead of getting the "active menu id" it will work, but I can do this without change the Joomla code? 我知道如果API只是忽略“项目菜单ID”而不是获取“活动菜单ID”它将起作用,但我可以在不更改Joomla代码的情况下执行此操作吗? Also, I want that the "item menu id" continue to be considered for the cases where an article actually belongs to a menu, so the generated URL will be SEF. 此外,我希望在文章实际属于菜单的情况下继续考虑“项目菜单ID”,因此生成的URL将是SEF。

There is any way to solve this? 有什么方法可以解决这个问题吗? Or every article must belong to a menu item? 或者每篇文章都必须属于菜单项?

My Joomla version is 2.5.13 我的Joomla版本是2.5.13

If you're trying to generate a URL based on the article ID ( $articleID in your code), your query should look something like the following: 如果您尝试根据文章ID(代码中的$articleID )生成URL,则查询应如下所示:

select id from #__menu where link='index.php?option=com_content&view=article&id=$articleID';

For this example we'll assume to you stored the result in $menuID , or that $hasMenuID===false . 对于此示例,我们假设您将结果存储在$menuID ,或者$hasMenuID===false You'll then determine your output URL like so: 然后,您将确定输出URL,如下所示:

if($hasMenuID===false) $outURL = "index.php?option=com_content&id=$articleID";
else $outURL = "index.php?option=com_content&itemid=$menuID";

If you want nice SEF-friendly URLs you can then apply JRoute . 如果您想要不错的SEF友好URL,您可以申请JRoute Also, please don't forget to typecast your $articleID to an integer before your initial query. 此外,请不要忘记在初始查询之前将$ articleID强制转换为整数。 SQL safety and all. SQL安全等等。

Every article need not be linked to menu to make it work. 每篇文章都不需要链接到菜单以使其工作。

You would want to run the url through JRoute to get the full url. 您可能希望通过JRoute运行URL以获取完整的URL。

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

You can also redirect a url to another url with .htaccess There are also other extensions out there, which can help you redirect to a url. 您还可以使用.htaccess将网址重定向到另一个网址。还有其他扩展程序,可以帮助您重定向到网址。

For the rest of the url you would need to know the component you are calling (added as the option like above, Content manager is always com_content) and the view (which for an article is article). 对于网址的其余部分,您需要知道您正在调用的组件(添加为上面的选项,内容管理器始终是com_content)和视图(对于文章是文章)。 You can see the different views available for com_content by checking the file structure under /components/com_content/views/. 您可以通过检查/ components / com_content / views /下的文件结构来查看com_content可用的不同视图。 Besides Article, you should see articles, categories, and category as well as a few other. 除了文章,你应该看到文章,类别和类别以及其他一些。

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

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