Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Bootstrap navbar active class in Laravel 5

I've been wondering around looking for solutions, but can't really understand especially when creating helpers. I'm new in Laravel and I want a simple or if not a detailed instruction on how to set the active class for my bootstrap navbar.

Here's what I've done so far, but can't get it done:

<div class="header clearfix">         <nav>           <ul class="nav nav-pills pull-right">             <li class=""><a href="{{ url('/') }}">Home</a>             </li>             <li {{ Request::is('about*') ? ' class="active"' : null }}><a href="{{ url('about') }}">About Us</a>             </li>             <li><a href="{{ url('auth/login') }}">Login</a>             </li>         </ul>     </nav>     <h2 class="">Tobacco Prevention and Control Program</h2> </div> 

EDIT

Setting class="active" will make all nav-pills active. The intended effect is that only the li of the current page have the active class.

like image 631
Ikong Avatar asked Apr 24 '15 02:04

Ikong


People also ask

How set navbar active class in bootstrap?

To accomplish this task, you can use ng-controller(NavigationController) to set the bootstrap navbar active class with AngularJS. To run a single controller outside ng-view. You can set class= “active” when the angular route is clicked.

Is active in laravel?

Active is a helper package for Laravel that makes it easy to recognize the current path or route, useful for adding 'active' classes (like those used in the Boostrap framework) and performing other actions only when a certain route is active.


1 Answers

Your code is working fine, but you have to use it for every link that can be active. It is better to return only class name, not class="..." so you can add other classes.

Be careful when using * at the end (about*). If you use /* for home page then it will always be marked as active (because every other page starts with /).

<ul class="nav nav-pills pull-right">     <li class="{{ Request::is('/') ? 'active' : '' }}">         <a href="{{ url('/') }}">Home</a>     </li>     <li class="{{ Request::is('about') ? 'active' : '' }}">         <a href="{{ url('about') }}">About Us</a>     </li>     <li class="{{ Request::is('auth/login') ? 'active' : '' }}">         <a href="{{ url('auth/login') }}">Login</a>     </li> </ul> 

You can also move {{ Request::is('/') ? 'active' : '' }} to helper function/method.

like image 63
Daniel Antos Avatar answered Sep 22 '22 11:09

Daniel Antos