5

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?

Joseph Chambers
  • 3,698
  • 4
  • 22
  • 37

3 Answers3

18

Or simply use a ternary operation:

<li {{ (Request::is('*login') ? 'class="active"' : '') }}>Login</li>
Nick Mitchell
  • 1,207
  • 13
  • 24
Lynnais
  • 189
  • 1
  • 3
10

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"
    ]
}
Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157
  • 2
    Hmm, Im new to laravel - where do I put a helper function? Can you link me to his video? Great answer though! – Joseph Chambers Apr 13 '14 at 12:24
  • 3
    https://laracasts.com/lessons/active-states I'm not sure if it is free tho, but laracasts is worth spending some $. And your helpers function goes anywhere you like, chech my edit in a sec – Jarek Tkaczyk Apr 13 '14 at 12:27
  • 1
    Sweet, yes, too bad he doesn't accept paypal as a subscription. Ive emailed support. I edited the composer.json and run composer but I get a error: http://cl.ly/image/3q3g3m2M0s0a - thats my composer file, and this is the error: http://cl.ly/image/3O0w1G253y3R – Joseph Chambers Apr 13 '14 at 12:38
  • 1
    you forgot about ',' between classmap and files array – Jarek Tkaczyk Apr 13 '14 at 12:44
  • 1
    Oh, I caught that just now too with http://jsonlint.com - but when I ran it got this: http://cl.ly/image/2o2I3l2f1J2f – Joseph Chambers Apr 13 '14 at 12:46
  • OK, I did everything you said.. with a clear composer update. Do I need to put anything in my app/config/app.php because it doesn't seem to be working. – Joseph Chambers Apr 13 '14 at 13:44
  • Check now, I made a slight mistake – Jarek Tkaczyk Apr 13 '14 at 14:04
0

Lynnais and Jarek Tkaczyk both have the correct answers. But the work around here is
create a file or class. If using class may create a static function with a name whatever you wish. And add the file name or class folder to the composer.json.

...
"autoload": {
    "classmap": [
        ... ,
        'app/helpers'
    ],
    "files": [
        "app/helpers.php"
    ]
}

Here, app/helpers is the folder containing classes that can be used later as the helper classes, and app/helpers.php is the plain PHP file with methods. See those commas before 'app/helpers' and "files". Create a class like this.

namespace App\Helper;
class Helper{
    public static function set_active($route){
        return (\Request::is($route.'/*') || \Request::is($route)) ? "active" : '';
    }
}

Now call it whenever you want like this from views

<ul class="nav navbar-nav">
    <li class="dropdown {{ \App\Helper\Helper::set_active('/') }}"> {{ HTML::decode(HTML::link('/','<i class="fa fa-home"></i> Home  ', array('class' => 'dropdown-toggle'))) }} </li>
    <li class="dropdown {{ \App\Helper\Helper::set_active('admins') }}"> {{ HTML::decode(HTML::link('/admins','<i class="fa fa-user"></i> Admins  ', array('class' => 'dropdown-toggle'))) }} </li>                
</ul>

OR

<li class="{{\App\Helper\Helper::set_active('/')}}">Home</li>
<li class="{{\App\Helper\Helper::set_active('admins')}}">Admins</li>

Happy coding. :)

ssi-anik
  • 2,998
  • 3
  • 23
  • 52