I'm building a component for Joomla! 2.5 and inside I'm using JRoute::_('index.php?option=com_myapp&view=cpanel')
to build all my links. This works, BUT it produces links which look like that:
/component/myapp/cpanel.html
In the menu however I have defined that index.php?option=com_myapp&view=cpanel
has an alias of "myapp" so the link should rather be
/myapp/cpanel.html
The component is accessible via this path. If I do that, also all links generated inside will have the /myapp
prefix. But for use in a template (special login link) and if the user stumbles upon /component/myapp
.... I still want all links to go to /myapp
prefix.
How can I force JRoute to use this menu item entry by itself?
//look if there is a menu item set for myapp. if yes, we use that one to redirect
$db = JFactory::getDBO();
$defaultRedirect = 'index.php?option=com_myapp&view=cpanel';
$db->setQuery('SELECT `id` FROM #__menu WHERE `link` LIKE '. $db->Quote($defaultRedirect) .' LIMIT 1' );
$itemId = ($db->getErrorNum())? 0 : intval($db->loadResult());
if($itemId){
$rpath = JRequest::getString('return', base64_encode(JRoute::_('index.php?Itemid='.$itemId)));
}else{
$rpath = JRequest::getString('return', base64_encode(JRoute::_('index.php?option=com_myapp&view=cpanel')));
}
Beware: This is NOT language-safe. It takes the first menu entry found in the db. If you setup different menu aliases for different languages you have to check for the right language in the SQL query, too.