in my master.blade.php file I have a navigation which Im trying to put together.
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="col-sm-5">
<ul class="nav navbar-nav" >
<li class="{{ Route::current( 'home.index') ? 'active' : '' }}">{{ HTML::linkAction('HomeController@index', 'Home') }}
<li><a href="">Locations</a></li>
<li class="{{ Route::current( 'order.index') ? 'active' : '' }}">{{ HTML::linkAction('OrderController@index', 'Order Online') }}
</ul>
</div>
<div class="col-sm-5 navbar-right">
<ul class="nav navbar-nav navbar-right">
<li class="{{ Route::current( 'menu.index') ? 'active' : '' }}">{{ HTML::linkAction('MenuController@index', 'Menu') }}
<li><a href="">About</a></li>
<li><a href="">Contact Us</a></li>
</ul>
</div>
<div class="container-fluid">
</nav>
The problem here is that all of them have the li set to active. Not the current page. What am I doing wrong?
Or simply use a ternary operation:
<li {{ (Request::is('*login') ? 'class="active"' : '') }}>Login</li>
This piece returns object
Route::current( 'home.index')
change it to
Route::currentRouteName() == 'home.index'
And I suggest making a helper function of it, something looking like this:
function setActive($route, $class = 'active')
{
return (Route::currentRouteName() == $route) ? $class : '';
}
Credits for the above to Jeffrey at http://laracasts.com
You can place your helpers for example in app/helpers.php, then you need to add it to autoload in /composer.json like so
...
"autoload": {
"classmap": [
...
],
"files": [
"app/helpers.php"
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With