, (*1)
Navigation
After filling the navigation with your menu / breadcrumb - information (e.g. categories or whatever), you´re able to
create a fully rendered HTML list., (*2)
Installation
Pull in the package with composer:, (*3)
{
"require": {
"bkoetsier/navigation": "1.0.*"
}
}
Laravel user
If you´re using the Laravel framework, all you have to do is reference the service provider and you are ready to go !, (*4)
// app/config/app.php
'providers' => [
'...',
'Bkoetsier\Navigation\Providers\Laravel\NavigationServiceProvider',
];
Without Laravel
If you aren´t using Laravel, you have to wire up the package yourself:, (*5)
use Bkoetsier\Navigation\Navigation;
use Illuminate\Support\Collection;
$nav = new Navigation(new Bucket(new Collection));
In case of using Laravel Navigation is shared via the IoC Container, so if you hydrate a bucket once, the bucket will be available all over your app,
but the main difference is in instantiation, (*6)
Basic usage
First step is always hydrating the bucket. To do so, you have to provide an array of objects (preferably StdClass)
with the following properties:
- itemId unique identifier like the database index
- itemContent the content that should be rendered for this item
- parentId identifier of the parent, (*7)
In the fill method you are able to rename each parameter to your corresponding properties :, (*8)
$example = [
[
"id": 1,
"content": '<a href="/books">Books</a>',
"parent": null,
],
[
"id": 2,
"content": '<a href="/books/fiction">Fiction</a>',
"parent": 1,
]
];
// array to \Std
$data = json_decode(json_encode($example));
// with Laravel
Nav::fill($data, $itemIdentifier = 'id', $itemContent ='content',$parentIdentifier = 'parent');
//without Laravel
$nav->fill($data, $itemIdentifier = 'id', $itemContent = 'content',$parentIdentifier = 'parent');
After hydrating you have to set the current item-id and call the render-method:, (*9)
//with Laravel
// set the current active item-id, maybe from a url or db
Nav::setCurrent(2);
Nav::menu('main')->render();
//without Laravel
$nav->setCurrent(2);
$nav->menu('main')->render();
Attention: when you use php Nav::setCurrent() it will be set for each menu you have defined !, (*10)
will output:, (*11)
<ul>
<li>
<a href="/books">Books</a>
<ul>
<li>
<span class="active"><a href="/books/fiction">Fiction</a></span>
</li>
</ul>
</li>
</ul>
Please note that the current item will be wrapped in a span.active for additional styling
Books has a Level of 1, (*12)
Books has a Level of 1 not 0 !, (*13)
If you have multiple navigation on your site you can set different states for each one:, (*14)
Nav::menu('main')->setCurrent(1)->setMaxLevel(1);
Nav::menu('sub')->setCurrent(2);
//will render until level == 1 from id 1 down
Nav::menu('main')->render();
// will render from id 2 down until the end
Nav::menu('sub')->render();
For the breadcrumbs you just call the same bucket:, (*15)
//with Laravel
Nav::breadcrumbs()->render();
//without Laravel
$nav->breadcrumbs()->render();