How to create a dynamic menu in Symfony
While I do find that the PHP Symfony framework is wonderful for many things, I am finding it difficult to create easily scalable dynamic menus for a website.
I am wondering how other Symfony developers out there have dealt with this issue. I will give a simple example as to what I am asking.
If go to www.fool.com you will see that they have a typical menu setup. A list of categories with one of them being the active one that is in some way highlighted with CSS.
What would be a good way to make this dynamically created in a Symfony project. Passing variables of some sort from module to module. I have not yet found an elegant solution.
Generally, I just manually build an array of module/action combinations that I'd like to appear in my menu. It'd be fairly easy to grab this information from a dynamic source (like a configuration file, or a database):
$this->menu = array( 'Home' => array('default', 'index'), 'About Us' => array('default', 'aboutUs'), 'Products' => array('products', 'index') );
Then, I use the controller's getActionName and getMethodName methods to determine which item in my menu is the current one:
$this->current = array_search(array($this->getModuleName(), $this->getActionName()), $this->menu);
Then I can iterate over $menu, from within the template, to build up the menu. And I can compare the items with $current, to determine whether I should set a "current" class on the menu item's <li>.
The fool.com example you give is a bit more complicated, because it's two level. In that case, you'd probably want to be generating your menu tree from a database, perhaps making use of Propel's nested set support.