有没有更好的方法来构建这个 joomla url?

发布于 2024-08-10 07:39:23 字数 977 浏览 8 评论 0原文

到目前为止,我正在以最不舒服的方式重新发明轮子。我的直觉告诉我,有一天它会破裂,给我带来很大的痛苦。因此,我正在寻找一种更好的方法来获取文章别名并构建菜单项 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦行七里 2024-08-17 07:39:23

使用 JRoute?假设您仍然从别名开始,则更“Joomla”的方式可能类似于:

/* Find article by alias */
$db =& JFactory::getDBO();
$sql = 'select id from #__content where alias=' . $db->quote($alias);
$db->setQuery($sql);
$row = $db->loadAssoc();
$artId = $row['id'];
if ($artId != null) {
  $articleUrl = JRoute::_('index.php?option=com_content&view=article&id=' . $artId);
}

这里的 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:

/* Find article by alias */
$db =& JFactory::getDBO();
$sql = 'select id from #__content where alias=' . $db->quote($alias);
$db->setQuery($sql);
$row = $db->loadAssoc();
$artId = $row['id'];
if ($artId != null) {
  $articleUrl = JRoute::_('index.php?option=com_content&view=article&id=' . $artId);
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文