Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel get current route

Tags:

html

php

laravel

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.

like image 402
wobsoriano Avatar asked Feb 09 '23 02:02

wobsoriano


2 Answers

$uri = Request::path()

Check out the docs

like image 189
Chris Avatar answered Feb 11 '23 19:02

Chris


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>
like image 35
Basit Avatar answered Feb 11 '23 17:02

Basit