I have some navigation links and I want to get the current route in laravel 5 so that I can use the class
active
in my navigations. Here are the sample links:
<li class="active"> <a href="/">Home</a> </li>
<li> <a href="/contact">Contact</a> </li>
<li> <a href="/about">About</a> </li>
As you can see, in the Home
link there is the active class
which means that it is the current selected route. But since I am using laravel blade
so that I would not repeat every navigation links in my blades, I can't use the active class because I only have the navigation links in my template.blade.php
. How can I possibly get the current route and check if it is equal to the current link? Thank you in advance.
$uri = Request::path()
Check out the docs
You can use named routes like
Route::get('/', ['as'=>'home', 'uses'=>'PagesController@index']);
and then in the view you can do the following
<li {!! (Route::is('home') ? 'class="active"' : '') !!}>
{!! link_to_route('home', 'Home') !!}
</li>
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