HTML Navigation Module for Kohana 3.3
Simplify rendering, building and maintenance of simple, dynamic standardised navigation menus and breadcrumbs. Instead of ..., (*1)
<? if ($user->get_role() === Role::ANONYMOUS):?>
<ul>
<li><a href="/" <?= $page === 'home' ? 'class="active"' : NULL?>>Home</a></li>
<li><a href="/about" <?= $page === 'about' ? 'class="active"' : NULL?>>About</a></li>
</ul>
<? // elseif (...)?>
... we do this:, (*2)
<?
return [
'items' => [
'url' => 'home',
'title' => 'Home',
],
[
'route' => 'about',
'title' => 'About',
],
];
As you can see the first item uses an url property, as the original Kohana-menu module required, while the second item uses a route property., (*3)
The route property was added to make use of Kohana's own routing system and use reverse routing, making it easier to check if the link is currently active., (*4)
I do recomend defining routes for every controller's action you have in your project., (*5)
Installation
Place the files in your modules directory.
As a Git submodule:
git clone git://github.com/happyDemon/elements.git modules/elements
{
"require": {
"php": ">=5.4.0",
"composer/installers": "*",
"happyDemon/elements":"*"
}
}
Copy MODPATH.elements/config/navigation/navbar.php
into APPPATH/config/navigation/navbar.php
and customize
Activate the module in bootstrap.php
.
<?php
Kohana::modules(array(
...
'elements' => MODPATH.'elements',
));
Example use case
A WordPress type blog might have..., (*6)
- Public main navigation menu
- Public footer menu
- Admin-only menu on the public pages, when admin is logged in
- Admin-only menu on the administrator interface
Normally, you'd build HTML views with ul
and li
elements and then write some PHP to highlight the active link. This is
difficult to maintain (DRY) and too much hassle (not to mention ugly)., (*7)
Instead, describe your (standardised) menus in configuration files and have Kohana do the heavy lifting., (*8)
The same goes for breadcrumbs, you might need them for:, (*9)
- Administration panel
- Forum
- Catalogs
and could have the same structure you'd use for your menu's, (*10)
Kohana-menu has a specific job and does it well, I personally also needed a way to manage my breadcrumbs for certain areas of the site., (*11)
Since my breadcrumbs contain the same data structure as my menu's (and many could share 1 config file), I decided to expand on this module so I could use it for both ends., (*12)
Basics
You define your menus in Kohana configuration files
(see config/navigation/navbar.php).
Then, in your (main) controller (or template), you construct a new Element object, set the active link and render it in your template. Done., (*13)
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<?=Element::factory('navbar')->render('Menu', 'bootstrap/navbar')?>
</div>
</div>
</div>
and output the breadcrumb wherever you want (we'll use the bootstrap styling), (*14)
<?=Element::factory('navbar')->render('Breadcrumb', 'bootstrap')?>
You might wish to instantiate the menu in your main controller, since this gives you a way to interact with the Element object
before it's rendered., (*15)
Config files
You can use different config files by setting the factory's $config
parameter., (*16)
$element = Element::factory($role); // this could use `config/navigation/(user|admin).php`
Use set_current()
to mark the current menu item in your controller if you're not making use of routes, (*17)
$element->set_current('article/show');
The parameter of set_current()
is the URL value of the respective item or its (numeric) array key., (*18)
However, if you've defined a 'route' property in the item it can be auto-detected if you define all your routes using Route::set in your bootstrap.php or init.php's, (*19)
Hard-set the active breadcrumb item
On certain pages you'd like to show a dynamic active breadcrumb item (the last one down the tree) to clearify which action if being done., (*20)
You can do this by calling last_item
on your Element
object and defining what you'd want to be rendered instead of the default text that was defined in your config file:, (*21)
$element->last_item('editing user "happyDemon"');
Documentation
The code is mostly commented, a user guide is on it's way with more examples., (*22)
Licence
The Kohana module started out as a fork of Anroot's Kohana-menu, which in turn is a fork of the original Kohana Menu module by
Bastian BrÀu, but is now independently developed under the MIT licence., (*23)
, (*24)