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 detaile
Set a section on your blade file (let home.blade.php) like
@section('Home', 'my-active-class')
And set a section on your another blade file (let about.blade.php) like
@section('About', 'my-active-class')
and yield this section on app.blade.php (Suppose you are extending from app.blade.php)
...
<li class="@yield('Home')"><a href="#">Home</a></li>
<li class="@yield('About')"><a href="#">About</a></li>
...
This is simple: to get your links to be active when using bootstrap, all you need is an if statement inside the class link, for instance: i have my current url as http://example.com/home
<li class="{{ Request::url() == url('/home') ? 'active' : '' }}"><a href="/home" ></li>
Home
</a>
and you are good to go.
I found the solution:
composer require devmarketer/easynav
More details : https://github.com/DevMarketer/LaravelEasyNav
If you are working with named routes. You can use this approach in your views:
{{ Route::currentRouteNamed('about') ? 'active' : '' }}
or
{{ Route::is('about') ? 'active' : '' }}
The Illuminate\Routing\Router#is(...)
is an alias of the Illuminate\Routing\Router#currentRouteNamed(...)
.
<ul class="nav nav-second-level">
<li class="{{ Request::segment(1) === 'programs' ? 'active' : null }}">
<a href="{{ url('programs' )}}" ></i> Programs</a>
</li>
<li class="{{ Request::segment(1) === 'beneficiaries' ? 'active' : null }}">
<a href="{{url('beneficiaries')}}"> Beneficiaries</a>
</li>
<li class="{{ Request::segment(1) === 'indicators' ? 'active' : null }}">
<a href="{{url('indicators')}}"> Indicators</a>
</li>
</ul>
<ul class="nav navbar-nav 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('whyus') ? 'active' : '' }}">
<a href="{{ url('/whyus') }}">Why Us</a>
</li>
</ul>