1

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

http://mysite.com/previous-menu-item-alias?itemId=101

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/…? – Valentin Despa Jun 8 '14 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. – Mathew Lenning Jun 8 '14 at 10:08
3

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.

Example:

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

$this->setRedirect(JRoute::_($item->route));
$this->redirect();


//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 = '';
}

$this->setRedirect(JURI::base().$route);
$this->redirect();

// 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, privacy policy and cookie policy

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