MenuHelper
A tool for building and rendering lists of links and elements. This helper will let you build up static and context sensitive links in your views and then render them all as part of the layout. It is powerful enough to do multiple, nested lists. Created for the purpose of main, sub and context sensitive menues, this helper can also be used as an html UL generator.
Installation and requirements
- Copy code to a file app/views/helpers/menu.php
- add 'Menu' to AppController's $helpers property
To understand how this helper works there are two important concepts. Firstly, in a single run of cakephp, only one instance of any helper is used. There for we can temporarily "store" information in it (as a property) between views, elements and layouts. In the most common use of this helper, links are created in the view and in elements and then the layout renders them. The reason why this works is the second important concept; the layout is rendered after the view. Therefore we can add to the list of urls in when the layout is rendered, the menu helper already know all that it is to render.
Comments
Comment
1 Array-keys must be string
A working example of a 3-level menu with keys:
$menu->add('tree', array('root1', array('controller'=>'root', 'action'=>'view', 1)));
$menu->add(array('tree', 'branch1'), array('branch1name', array('controller'=>'branch', 'action'=>'view', 1)));
$menu->add('tree', array('root2', array('controller'=>'root', 'action'=>'view', 2)));
$menu->add(array('tree', 'branch2'), array('branch2name', array('controller'=>'branch', 'action'=>'view', 2)));
$menu->add(array('tree', 'branch3'), array('branch3name', array('controller'=>'branch', 'action'=>'view', 3)));
$menu->add(array('tree', 'branch2', 'leaf1'), array('leaf1name', array('controller'=>'leaf', 'action'=>'view', 1)));
$menu->add(array('tree', 'branch2', 'leaf2'), array('leaf2name', array('controller'=>'leaf', 'action'=>'view', 2)));
echo $menu->generate('tree');
Great Helper once you get to know it !
Comment
2 Lol what a title =)