Laravel helper that detects active navigation menu items and applies bootstrap classes.
I reuse this code across many projects so I wanted a central place for it.
First, pull in the package through Composer.
"require": {
"laravelista/ekko": "~1.2"
}
And then, if using Laravel 5 or 4, include the service provider within app/config/app.php
.
'providers' => [
'Laravelista\Ekko\EkkoServiceProvider'
];
And, for convenience, add a facade alias to this same file at the bottom:
'aliases' => [
'Ekko' => 'Laravelista\Ekko\Facades\Ekko'
];
You would most likely use this package in your navbar
partial like so:
<li>
<a href="{{ route('home') }}" class="{{ Ekko::isActiveRoute('home') }}">
<i class="halflings white home"></i> Home
</a>
</li>
<li>
<a href="#" class="{{ Ekko::areActiveRoutes(['murter', 'kornati']) }}">
<i class="halflings white screenshot"></i> Location
</a>
<ul>
<li>
<a href="{{ route('murter') }}">Murter</a>
</li>
<li>
<a href="{{ route('kornati') }}">Kornati</a>
</li>
</ul>
</li>
<li>
<a href="{{ route('trips.index') }}" class="{{ Ekko::isActiveMatch('trips') }}">
<i class="halflings white road"></i> Trips
</a>
</li>
As the second parameter to any method, you can pass the value you want to get returned if there was a match. By default this is active
which is Bootstrap default.
Compares given route name with current route name.
{{ Ekko::isActiveRoute('home') }}
The *
wildcard can be used for resource routes.
{{ Ekko::isActiveRoute('user.*') }}
Compares given URL with current URL.
{{ Ekko::isActiveURL('/about') }}
Detects if the given string is found in the current URL.
{{ Ekko::isActiveMatch('bout') }}
Compares given array of route names with current route name.
{{ Ekko::areActiveRoutes(['product.index', 'product.show']) }}
The *
wildcard can be used for resource routes, including nested routes.
{{ Ekko::areActiveRoutes(['user.*', 'user.comments.*']) }}
Compares given array of URLs with current URL.
{{ Ekko::areActiveURLs(['/product', '/product/create']) }}