Dynamic Menu Builder for Bootstrap 3: Menu Manager

Posted by Unknown on Friday, June 20, 2014

Creating menus and navigation bars has never been easier than with Twitter Bootstrap. We can easily create stylish navigation bars without too much effort. While it’s enough for some projects, you might encounter situations when you need to have more control over the links and items inside your menu.

Imagine you need to load the items from a database table. What if you need to limit the items to a group of users having the required permissions?

This is where static solutions can’t help much, so, we should think of a more dynamic approach.


In this tutorial I’m going to show you how to create your own dynamic menu builder in PHP. This is a two part series, with the first part focusing on demo code and the Menu class, and the second part taking care of the other classes and usage examples.


Defining The Goal


Before we start, let’s briefly outline what we need to accomplish. Personally I like to get more with writing less and I’m sure you do as well.


We should be able to create menus the easy way, but the code should stay clean and professional, written in modern object oriented style, like so:



<?php

//create the menu
$menu
= new Menu;

//Add some items
$menu
->add('Home', '');
$menu
->add('About', 'about');
$menu
->add('Services', 'services');
$menu
->add('Portfolio', 'portfolio');
$menu
->add('contact', 'contact');

?>


We should be able to add sub items in a semantic way rather than explicitly defining a pid for each sub item:



<?php
//...
$about
= $menu->about('about', 'About');
$about
->add('Who we are?', 'who-we-are');
$about
->add('What we do?', 'what-we-do');
//...
?>


We should provide a way for adding html attributes to the items:



<?php
//...
$menu
->add('About', array('url' => 'about', 'class'


Truncated by Planet PHP, read more at the original (another 799 bytes)




more

{ 0 comments... » Dynamic Menu Builder for Bootstrap 3: Menu Manager read them below or add one }

Post a Comment

Popular Posts