Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

active links in laravel 4

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?

like image 975
Joseph Chambers Avatar asked Apr 13 '14 11:04

Joseph Chambers


2 Answers

Or simply use a ternary operation:

<li {{ (Request::is('*login') ? 'class="active"' : '') }}>Login</li>
like image 107
Lynnais Avatar answered Jan 03 '23 14:01

Lynnais


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"
    ]
}
like image 20
Jarek Tkaczyk Avatar answered Jan 03 '23 16:01

Jarek Tkaczyk