Standalone Menu's With PHP

The era of standalone components is here. Yes people will definitely take and promote standalone components . Lets have a look into the KnpMenu . Its awesome that you can create awesome menu . Yes with PHP and after all there is no dependencies . Lets see some examples {syntaxhighlighter class=“php”} use Knp\Menu\MenuFactory; use Knp\Menu\Renderer\ListRenderer; $factory = new MenuFactory(); $menu = $factory->createItem(‘My menu’); $home = $menu->addChild(‘Home’, array(‘uri’ => ‘/’)); $home1 = $home->addChild(‘ Home1’, array(‘uri’ => ‘/about’)); $home1->addChild(‘Company’, array(‘uri’ => ‘/about/company’)); $home1->addChild(‘Life’, array(‘uri’ => ‘/about/life’)); $home1->addChild(‘Work’, array(‘uri’ => ‘/about/work’)); $menu->addChild(‘Comments’, array(‘uri’ => ‘#comments’)); $menu->addChild(‘Aura’, array(‘uri’ => ‘http://auraphp.github.com/’)); $menu->addChild(‘Coming soon’); $renderer = new ListRenderer(); echo $renderer->render($menu); {/syntaxhighlighter} The above will give you the below output. {syntaxhighlighter class=“html”} – Home< Home1CompanyLifeWorkCommentsAura – Coming soon {/syntaxhighlighter} You can also create from an array. I was having some issue and Thank you Christophe Coevoet ( @Stof70 ) for quickly helping me. I was copying some test data and didn’t noticed one of the display value was hidden :P . So here it comes :–) . {syntaxhighlighter class=“php”} use Knp\Menu\MenuFactory; use Knp\Menu\Renderer\ListRenderer; $factory = new MenuFactory(); $array = array( ‘name’ => ‘test_menu’, ‘label’ => “Home”, ‘uri’ => ‘homepage’, ‘attributes’ => array(), ‘labelAttributes’ => array(), ‘linkAttributes’ => array(), ‘display’ => true, ‘displayChildren’ => true, ‘children’ => array( ‘jack’ => array( ‘name’ => ‘jack’, ‘label’ => “Jack”, ‘uri’ => ‘http://php.net’, ‘attributes’ => array(), ‘labelAttributes’ => array(), ‘linkAttributes’ => array(‘title’ => ‘php’), ‘display’ => true, ‘displayChildren’ => true, ‘children’ => array( ‘john’ => array( ‘name’ => ‘john’, ‘label’ => “John”, ‘uri’ => null, ‘attributes’ => array(), ‘labelAttributes’ => array(), ‘linkAttributes’ => array(), ‘display’ => true, ‘displayChildren’ => true, ‘children’ => array( ‘doe’ => array( ‘name’ => ‘doe’, ‘label’ => ‘doe’, ‘uri’ => ‘http://google.com’ ) ), ), ), ), ‘joe’ => array( ‘name’ => ‘joe’, ‘label’ => ‘test’, ‘uri’ => null, ‘attributes’ => array(‘class’ => ‘leaf’), ‘labelAttributes’ => array(‘class’ => ‘center’), ‘linkAttributes’ => array(), ‘display’ => true, ‘displayChildren’ => false, ‘children’ => array(), ), ), ); $menu = $factory->createFromArray($array); $renderer = new ListRenderer(); echo $renderer->render($menu); {/syntaxhighlighter} And this will print the below html. Consider making values hidden etc . You can notice how the output changes. {syntaxhighlighter class=“html”} – jack – john – doe – test {/syntaxhighlighter} There is Breadcrumb also in the latest 2.0 master branch ( Not yet released ? ) . I have not explored more how we can make it . I will update the post once I play with the Breadcrumbs. So good bye to Zend_Navigation. Its a new beginning for Knplabs , KnpMenu . Ryan Weaver deserves some credits for its a port of http://github.com/weaverryan/ioMenuPlugin ( symfony 1 plugin, but for the PHP community as a whole ). Source Code : http://github.com/knplabs/KnpMenu Its already having great documentation :) . Go and explore guys .. Look through tests if you have doubts ;–) . Ask support at Symfony irc . May be at a different one later ?

breadcrumb, knplabs, knpmenu, menu, php