如何在 Joomla 中使用 JRoute 路由到搜索菜单项?

发布于 11-26 05:05 字数 716 浏览 4 评论 0原文

我正在尝试在 Joomla 模板中创建一个框!这将显示所有关键字并将它们链接到适当的搜索页面。我有一个菜单项集,但是,我不想将菜单项硬编码到模板中,因此我想使用 JRoute 对象来生成 SEF url。

我正在使用这个函数:

JRoute::_('index.php?option=com_search&searchword='.$keyword);

或者这个:

JRoute::_('index.php?option=com_search&view=search&searchword='.$keyword);

但是,这会生成一个像这样的网址:

/component/search/?searchword=africa

当它应该创建一个像这样的搜索网址时:

/searchmenuitem?searchword=africa

我已经在网上进行了广泛的搜索,但没有找到这个问题的解决方案。任何想法将不胜感激。

好的,所以为您提供一些附加信息.. 我仅在尝试从 com_content 中的模板路由 URL 时遇到此问题。如果我尝试从 com_search 中的模板路由 url,一切都会完美运行。那么,com_content 是什么原因导致它无法正常工作呢?

谢谢! 大卫

I am trying to create a a box in a template in Joomla! that will display all of the keywords and link them to their appropriate search page. I have a menu item set, however, I don't want to hard-code the menu item into the template, so I want to use the JRoute object to generate the SEF url.

I am using this function:

JRoute::_('index.php?option=com_search&searchword='.$keyword);

or this:

JRoute::_('index.php?option=com_search&view=search&searchword='.$keyword);

however, this generates a url like this:

/component/search/?searchword=africa

when it ought to create a search url like this:

/searchmenuitem?searchword=africa

I have searched extensivly online and havn't found a solution to this problem. Any ideas would be greatly appreciated.

Ok, so some additional information for you.. I am only experiencing the problem when I try and route the URL from a template in com_content. If I try and route the url from a template in com_search everything works perfectly. So, what is it about com_content that is causing this to not work properly?

thanks!
david

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

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

发布评论

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

评论(3

淡笑忘祈一世凡恋2024-12-03 05:05:26

在 joomla 管理页面中,转到您为搜索结果页面选择的菜单项,并获取该菜单项的 ID (itemId)。

您可以尝试使用:

JRoute::_('index.php?option=com_search&view=search&Itemid=256&searchword=asdsadasdsa');

或者

JRoute::_('index.php?Itemid=256&searchword=asdsadasdsa');

两者都应该导致: /searchmenuitem.html?searchword=asdsadasdsa

编辑:
为了使其更舒适,您可以将 itemId 作为参数添加到模板中。

还有另一种方法,您可以从数据库中获取 itemId(多语言网站需要此方法)。如果您想要的话请告诉我。

编辑2:
这里是:

$db   =& JFactory::getDBO();
$lang =& JFactory::getLanguage()->getTag();
$uri  = 'index.php?option=com_search&view=search';

$db->setQuery('SELECT id FROM #__menu WHERE link LIKE '. $db->Quote( $uri .'%' ) .' AND language='. $db->Quote($lang) .' LIMIT 1' );

$itemId = ($db->getErrorNum())? 0 : intval($db->loadResult());

In joomla administration page go to the menu item you've chosen for the search results page and get the id of that menu item (itemId).

Than you can try using:

JRoute::_('index.php?option=com_search&view=search&Itemid=256&searchword=asdsadasdsa');

or even

JRoute::_('index.php?Itemid=256&searchword=asdsadasdsa');

both should result in: /searchmenuitem.html?searchword=asdsadasdsa

EDIT:
To make it more comforable you could add itemId as a param to your template.

There is another way, where u can get the itemId from the database (this method is required on multilingual websites). Let me know if you want it.

EDIT2:
Here it is:

$db   =& JFactory::getDBO();
$lang =& JFactory::getLanguage()->getTag();
$uri  = 'index.php?option=com_search&view=search';

$db->setQuery('SELECT id FROM #__menu WHERE link LIKE '. $db->Quote( $uri .'%' ) .' AND language='. $db->Quote($lang) .' LIMIT 1' );

$itemId = ($db->getErrorNum())? 0 : intval($db->loadResult());
柏林苍穹下2024-12-03 05:05:26

我使用这种方法来获取特定组件和视图的菜单项 id

 function getSearchItemId() {

        $menu = &JSite::getMenu();
        $component = &JComponentHelper::getComponent('com_search');
        //get only com_search menu items
            $items  = $menu->getItems('componentid', $component->id);

        foreach ($items as $item) {
            if (isset($item->query['view']) && $item->query['view'] === 'search') {
                        return $item->id;

                    }
        }

        return false;

        }

然后我使用这种方法来获取 sef url

函数 getRouteUrl($route)
{

    jimport('joomla.application.router');

    // Get the global site router.
    $config = &JFactory::getConfig();
    $router = JRouter::getInstance('site');
    $router->setMode($config->getValue('sef', 1));

    $uri    = &$router->build($url);
    $path   = $uri->toString(array('path', 'query', 'fragment'));

    return $path;
}

这适用于任何模板。

像这样使用

$itemid = getSearchItemId();

//returns valid sef url
$url = getRouteUrl('index.php?Itemid='.$itemid);

你真的不需要在菜单表上执行sql来获取id。只需搜索菜单对象即可。

I use this kind of method to get a menu item id of specific component and view

 function getSearchItemId() {

        $menu = &JSite::getMenu();
        $component = &JComponentHelper::getComponent('com_search');
        //get only com_search menu items
            $items  = $menu->getItems('componentid', $component->id);

        foreach ($items as $item) {
            if (isset($item->query['view']) && $item->query['view'] === 'search') {
                        return $item->id;

                    }
        }

        return false;

        }

Then I use this method to get the sef url

function getRouteUrl($route)
{

    jimport('joomla.application.router');

    // Get the global site router.
    $config = &JFactory::getConfig();
    $router = JRouter::getInstance('site');
    $router->setMode($config->getValue('sef', 1));

    $uri    = &$router->build($url);
    $path   = $uri->toString(array('path', 'query', 'fragment'));

    return $path;
}

This just works in any template.

use like this

$itemid = getSearchItemId();

//returns valid sef url
$url = getRouteUrl('index.php?Itemid='.$itemid);

You really do not need to do sql on the menu table to get ids. Just search the menu object.

青巷忧颜2024-12-03 05:05:26

尝试在 joomla 后端创建新菜单,例如“隐藏菜单”。它永远不会显示在前面。但它会被JRoute使用然后添加到这个菜单中名为“searchmenuitem”的新菜单项,并链接到com_search。仅此而已。现在你可以调用它

JRoute::_('index.php?option=com_search&view=search&searchword=asdsadasdsa');

,它会被转换成这样

/searchmenuitem.html?searchword=asdsadasdsa

Try to create new menu in the joomla backend called for instance 'hidden-menu'. It will never be shown in the front. But it will be used by JRoute Then add to this menu new menuitem called 'searchmenuitem' with link to com_search. That is all. Now you can call

JRoute::_('index.php?option=com_search&view=search&searchword=asdsadasdsa');

and it will be ceonverted into this

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