获取 Joomla!来自 URL 的类别

发布于 2024-09-19 04:16:54 字数 259 浏览 2 评论 0原文

如何读取某个 URI 所属的部分?

我想增强 mod_breadcrumb 以将部分和类别放入 HTML 中。 JApplication->getPathway() 返回一个 JPathway,它基本上保存一个组合名称和 URL 的关联数组(如 $list[]->name 和 $list[]->link)。我认为,应该可以从链接获取部分和类别,但不知道如何。

一个起点可能是解析为 JURI-Object,但从那里我不知道如何进一步发展。有什么想法吗?

How can I read the section a certain URI belongs to?

I want to enhance the mod_breadcrumb to put section and category into the HTML. JApplication->getPathway() returns a JPathway which basically holds an assiciative array combining a name and an URL (as $list[]->name and $list[]->link). I think, it should be possible to get the section and category from a link, but don't know how.

A starting point could be the parsing into JURI-Object, but from there I don't know how get get further. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

放飞的风筝 2024-09-26 04:17:08

非常简单......

我假设您想添加文章的类别和部分,而不是您的自定义组件。

检查请求的当前 URL 是否用于文章。如果是文章,你知道文章ID,使用这个文章Id去数据库从#__content获取catid,使用这个cat_id转到#__categories并获取section(这是部分id),转到#__sections以获取正确的部分名称。所有这些都可以在 1 个 sql 语句中完成。

$breadcrumbs =& JFactory::getApplication()->getPathway();
$breadcrumbs->addItem("SECTION_NAME", JRoute::_("index.php?option=com_content&view=section&id=SECTION_ID"));
$breadcrumbs->addItem("CATEGOY_NAME", JRoute::_("index.php?option=com_content&view=category&id=CATEGORY_ID"));
$breadcrumbs->addItem("Article");

或者,如果您知道面包屑项目的 URL。你可以解析它并获取IDS。这里的技巧是不要通过 JFactory::getURI() 获取默认 URI 对象,因为事情会变得很难看,请使用 JFactory::getURI('YOU_URI_NAME')

<?php
//  You need to get Your own uri, you do not want to modify default URI
//  because this will messup a lot of things
$uri = JFactory::getURI('MyCustomURI');

//  Test # 1 [ID = SECTION_ID]
$url = "index.php?option=com_content&view=section&id=SECTION_ID";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');

//  Test # 2 [ID = 123]
$url = "index.php?option=com_content&view=section&id=123";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');
?>

Pretty straight forward...

I assume you want to add category and section for the article and not your custom component.

Check if requested current URL is for article. If it is for article you know the article ID, use this article Id to go database and get catid from #__content, Use this cat_id to go to #__categories and get section (this is section id), go to #__sections to get the proper section name. All this can be done in 1 sql statement.

$breadcrumbs =& JFactory::getApplication()->getPathway();
$breadcrumbs->addItem("SECTION_NAME", JRoute::_("index.php?option=com_content&view=section&id=SECTION_ID"));
$breadcrumbs->addItem("CATEGOY_NAME", JRoute::_("index.php?option=com_content&view=category&id=CATEGORY_ID"));
$breadcrumbs->addItem("Article");

Alternatively, if you know the URL from the breadcrumb item. You can parse it and get IDS. The trick here is not to get the default URI object by JFactory::getURI() because things will get ugly, use JFactory::getURI('YOU_URI_NAME').

<?php
//  You need to get Your own uri, you do not want to modify default URI
//  because this will messup a lot of things
$uri = JFactory::getURI('MyCustomURI');

//  Test # 1 [ID = SECTION_ID]
$url = "index.php?option=com_content&view=section&id=SECTION_ID";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');

//  Test # 2 [ID = 123]
$url = "index.php?option=com_content&view=section&id=123";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文