Navigation
Creates a HTML navigation menu and Breadcrumb menu from a PHP array, (*1)
Installation
Installation is available via Composer/Packagist, you can add the following line to your composer.json
file:, (*2)
"adamb/navigation": "^1.0"
or, (*3)
composer require adamb/navigation
Features
- Build HTML navigation from PHP array
- Can build multi-dimensional menus from multi-dimensional arrays
- Create breadcrumb menus
- Customisable HTML classes
- Change the current selected item (if not the current page)
License
This software is distributed under the MIT license. Please read LICENSE for information on the
software availability and distribution., (*4)
Usage
Menus and breadcrumbs can be created from either simple arrays or multi-dimensional arrays, (*5)
PHP Code
require 'src/navigation.php';
use Nav\Navigation;
$menu = array(
'Home' => '/',
'Link Text' => '/link-2',
'Sample' => '/sample',
'Another Page' => '/yet-another-link',
'Google' => 'https://www.google.co.uk',
'Final Link' => '/final-page',
);
// For this example we are saying we are on the home page of the website
// You should use something like $_SERVER['REQUEST_URI'] or filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL)
$currentURI = '/';
$navigation = new Navigation($menu, $currentURI);
echo($navigation->createNavigation());
Output
<ul class="nav navbar-nav">
<li class="active"><a title="Home" class="active">Home</a></li>
<li><a href="/link-2" title="Link Text">Link Text</a></li>
<li><a href="/sample" title="Sample">Sample</a></li>
<li><a href="/yet-another-link" title="Another Page">Another Page</a></li>
<li><a href="https://www.google.co.uk" title="Google">Google</a></li>
<li><a href="/final-page" title="Final Link">Final Link</a></li>
</ul>
PHP Code
require 'src/navigation.php';
use Nav\Navigation;
$menu = array(
'Home' => '/',
'Link Text' => '/link-2',
'Sample Submenu' => '/sample',
array(
'Sub item 1' => '/sub-pages/subpage1',
'Sub item 2' => '/sub-pages/subpage2',
'Has another level' => '/sub-pages/has-sub-pages',
array(
'Final Level' => '/sub-sub-pages/final-sub-level',
'Hello World' => '/sub-sub-pages/hello-world',
),
),
'Another Page' => '/yet-another-link',
'Google' => 'https://www.google.co.uk',
'Final Link' => '/final-page',
);
$currentURI = '/sub-sub-pages/hello-world'; // $_SERVER['REQUEST_URI']
$navigation = new Navigation($menu, $currentURI);
echo($navigation->createNavigation());
Output
PHP Code
require 'src/navigation.php';
use Nav\Navigation;
$menu = array(
'Home' => '/',
'Link Text' => '/link-2',
'Sample Submenu' => '/sample',
array(
'Sub item 1' => '/sub-pages/subpage1',
'Sub item 2' => '/sub-pages/subpage2',
'Has another level' => '/sub-pages/has-sub-pages',
array(
'Final Level' => '/sub-sub-pages/final-sub-level',
'Hello World' => '/sub-sub-pages/hello-world',
),
),
'Another Page' => '/yet-another-link',
'Google' => 'https://www.google.co.uk',
'Final Link' => '/final-page',
);
$currentURI = '/sub-sub-pages/hello-world'; // $_SERVER['REQUEST_URI']
$navigation = new Navigation($menu, $currentURI);
// Example 1
echo($navigation->createBreadcrumb());
// Example 2
echo($navigation->setBreadcrumbElement('ol')->createBreadcrumb(true, 'my-bc-class', 'bc-item'));
// Example 3
echo($navigation->setBreadcrumbElement('nav')->createBreadcrumb());
// Example 4
echo($navigation->createBreadcrumb(false));
Output
// Example 1
// Exmaple 2
- Home
- Sample Submenu
- Has another level
- Hello World
// Example 3
<nav class="breadcrumb">
<a href="/" title="Home" class="breadcrumb-item">Home</a>
<a href="/sample" title="Sample Submenu" class="breadcrumb-item">Sample Submenu</a>
<a href="/sub-pages/has-sub-pages" title="Has another level" class="breadcrumb-item">Has another level</a>
<span class="breadcrumb-item active">Hello World</span>
</nav>
// Example 4
<a href="/" title="Home">Home</a> > <a href="/sample" title="Sample Submenu">Sample Submenu</a> > <a href="/sub-pages/has-sub-pages" title="Has another level">Has another level</a> > Hello World
Change HTML Classes/Navigation ID
You can change the default class elements on the navigation and breadcrumb items by using the following commands, (*6)
PHP Code
require 'src/navigation.php';
use Nav\Navigation;
$menu = array(
'Home' => '/',
'Link Text' => '/link-2',
'Sample Submenu' => '/sample',
array(
'Sub item 1' => '/sub-pages/subpage1',
'Sub item 2' => '/sub-pages/subpage2',
'Has another level' => '/sub-pages/has-sub-pages',
array(
'Final Level' => '/sub-sub-pages/final-sub-level',
'Hello World' => '/sub-sub-pages/hello-world',
),
),
'Another Page' => '/yet-another-link',
);
$navigation = new Navigation($menu, '/sub-sub-pages/hello-world');
$navigation->setActiveClass('current-item c-item')
->setNavigationClass('my-nav-class')
->setNavigationID('my-unique-navigation-id')
->setDropDownClass('my-drop-down-class drop-down');
echo($navigation->createNavigation());
Output
Additional Features
You can also choose to display only a given number of navigation levels starting at any level you choose, (*7)
PHP Code
require 'src/navigation.php';
use Nav\Navigation;
$menu = array(
'Home' => '/',
'Link Text' => '/link-2',
'Sample Submenu' => '/sample',
array(
'Sub item 1' => '/sub-pages/subpage1',
'Sub item 2' => '/sub-pages/subpage2',
'Has another level' => '/sub-pages/has-sub-pages',
array(
'Final Level' => '/sub-sub-pages/final-sub-level',
'Hello World' => '/sub-sub-pages/hello-world',
),
),
'Another Page' => '/yet-another-link',
'Google' => 'https://www.google.co.uk',
'Final Link' => '/final-page',
);
$navigation = new Navigation($menu, '/sub-sub-pages/hello-world');
// Example 1
$levels = 1;
echo($navigation->createNavigation($levels));
// Example 2
$levels = 2;
$start_level = 1;
echo($navigation->createNavigation($levels, $start_level));
Output
// Example 1
// Only displays the top menu level
// Example 2
// Display 2 levels of the navigation starting at level 1 (the first sub level of the current item)