So I'm working on my a new component and I want to be able to route to a menu item when access is denied for a certain action.

I created a menu_id field in my table and I'm using the menuitem form field type to store the menu itemId.

I thought that I would be able to use the controllers setRedirect function like so

if($isRestricted && !$canDownload)
    $msg = $this->translate('COM_BABELU_PRODUCTS_DL_NOT_AUTHORISED');
    $this->setRedirect('index.php?itemId=101', $msg, 'warning');
    return false;

However it didn't work. it just appended the item id to the route


This didn't even load the view associated with the menu item with the ID 101.

Do I need to specify the entire route? I.E. lookup the link from the #__menus table and then call JRoute with that value?

If anyone has exp with this I would really appreciate any information.

----Edit --- I've done this before by querying the #__menus table directly and getting the link value then pushing that into JRoute. However I don't like the idea of querying the table directly because it creates a direct dependency. What I really want to know is how to get the link value without touching the #__menus table, or if there is a way to direct to a menu item using only the itemId.

  • Possible duplicate of: joomla.stackexchange.com/questions/662/…? Commented Jun 8, 2014 at 8:44
  • The question I'm asking is a little different. I don't need to find the itemId, because I have it stored in my database. Also I'm not trying to route to a view in my component, but any view that happens to be assigned to to the record. For instance com_content. Commented Jun 8, 2014 at 10:08

1 Answer 1


Okay. I found the answer.

First to get the item information without querying the #__menus table use this code:

$app = JFactory::getApplication();
$menu = $app->getMenu();
$item = $menu->getItem($itemId);

Now the redirect is a little difficult, because I want to use the proper route, but if I just send the route to JRoute, I end up with an error because JRoute just appends the url content to the current active item. The below example is being called from the controller.


 //current url = http://mysite.com/product
 //$item->route = my-page 


//resulting url = http://mysite.com/product/my-page

This of course throws a 404 because this route doesn't exist. "product" is part of my component "my-page" is a com_content article item.

However you can get around this by adding the URI:

$route = $item->route;
If((int)$item->home === 1)// its the home menu item, so clear the route
    $route = '';


// resulting url = http://mysite.com/my-page

I also tested this with the $item->link vs the $item->route and it seems to work the same. It is probably better to use the link, just in case the site isn't using search engine friendly URLs.

I hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.