Search code examples
joomlajoomla-extensionsjoomla1.6joomla-templatejoomla-sef-urls

How do you use JRoute in Joomla to route to a Search menu item?


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


Solution

  • 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());