有没有更好的方法来构建这个 joomla url?
到目前为止,我正在以最不舒服的方式重新发明轮子。我的直觉告诉我,有一天它会破裂,给我带来很大的痛苦。因此,我正在寻找一种更好的方法来获取文章别名并构建菜单项 url 或文章 url。是否没有 joomla api 调用可以使这变得更容易/更干净/更面向未来?
/* Find article by alias */
$db =& JFactory::getDBO();
$sql = 'select id from #__content where alias=' . "'$alias'";
$db->setQuery($sql);
$row = $db->loadAssoc();
$artId = $row['id'];
if ($artId != null) {
$artLink = 'index.php?option=com_content&view=article&id='.$artId;
/* Find menu item by article id */
$sql = 'select parent,alias from #__menu where link=' . "'$artLink'";
$db->setQuery($sql);
$row = $db->loadAssoc();
$menuLink = '';
while ($row != null) {
$menuLink = '/' . $row['alias'] . $menuLink;
$sql = 'select parent,alias from #__menu where id=' . $row['parent'];
$db->setQuery($sql);
$row = $db->loadAssoc();
}
$menuLink = 'index.php' . $menuLink;
}
$articleUrl = ($menuLink != '') ? 'index.php' . $menuLink : JRoute::_($artLink);
So far I'm re-inventing the wheel here in the most uncomfortable way. I can feel in my gut that this will break one day and cause me a lot of pain. Therefore I'm looking for a better way to take an article alias and build either the menu item url or the article url. Are there no joomla api calls that make this easier/cleaner/more future-proof?
/* Find article by alias */
$db =& JFactory::getDBO();
$sql = 'select id from #__content where alias=' . "'$alias'";
$db->setQuery($sql);
$row = $db->loadAssoc();
$artId = $row['id'];
if ($artId != null) {
$artLink = 'index.php?option=com_content&view=article&id='.$artId;
/* Find menu item by article id */
$sql = 'select parent,alias from #__menu where link=' . "'$artLink'";
$db->setQuery($sql);
$row = $db->loadAssoc();
$menuLink = '';
while ($row != null) {
$menuLink = '/' . $row['alias'] . $menuLink;
$sql = 'select parent,alias from #__menu where id=' . $row['parent'];
$db->setQuery($sql);
$row = $db->loadAssoc();
}
$menuLink = 'index.php' . $menuLink;
}
$articleUrl = ($menuLink != '') ? 'index.php' . $menuLink : JRoute::_($artLink);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 JRoute?假设您仍然从别名开始,则更“Joomla”的方式可能类似于:
这里的 JRoute 文档:
http://api.joomla.org/Joomla-Framework/JRoute.html
这也可以解决 SQL 转义问题,如果您的别名中有引号,您可能会遇到这些问题;o
我还应该提到,如果您特别想要菜单链接 - 您需要在末尾添加“&itemid=”位传递给 JRoute 的路径!当然,您无法从别名中获取该 itemid - 可以有多个菜单项指向同一篇文章;)。
Use JRoute? Assuming you're still starting with an alias, a more 'Joomla' way do this could be something like:
Docs for JRoute here:
http://api.joomla.org/Joomla-Framework/JRoute.html
This also gets around SQL escaping issues that you could get if there were quotes in your alias ;o
I should also mention that if you want the menu link specifically - you need to have the '&itemid=' bit on the end of the path passed to JRoute! Of course, you can't get that itemid from the alias - there can be multiple menu items pointed to the same article ;).