Kohana 3: How to find the active item in a dynamic menu

Posted by Svish on Stack Overflow See other posts from Stack Overflow or by Svish
Published on 2010-03-11T20:11:13Z Indexed on 2010/03/11 20:14 UTC
Read the original article Hit count: 659

Filed under:
|
|

Maybe not the best explanation, but hear me out. Say I have the following in a config file called menu.php:

// Default controller is 'home' and default action is 'index'
return array(
   'items' => array(
      'Home' => '',
      'News' => 'news',
      'Resources' => 'resources',
   ),
);

I now want to print this out as a menu, which is pretty simple:

foreach(Kohana::config('menu.items') as $title => $uri)
{
    echo '<li>' . HTML::anchor($uri, $title) . '</li>';
}

However, I want to find the $uri that matches the current controller and action. And if the action is the default one or not. What I want to end up with is that menu item should have id="active-item" if it is the linking to the current controller, but the default action. And id="active-subitem if it is linking to the current controller and the action is not the default one. Hope that made sense...

Anyone able to help me out here? Both in how to do this in Kohana 3 and also how it should be done in Kohana 3. I'm sure there are lots of ways, but yeah... any help is welcome :)


Examples:

  • domain.com -- Home should be active-item since it is the default controller
  • domain.com/home -- Home should be active-item
  • domain.com/home/index -- Home should be active-item since index is the default action
  • domain.com/resources -- Resources should be active-item
  • domain.com/resources/get/7 -- Resources should be active-subitem since get is not the default action

© Stack Overflow or respective owner

Related posts about php

Related posts about kohana-3