Posts Learn Components Snippets Categories Tags Tools About
/

How to Get Currently Active Route in Laravel

Learn how to get the currently active route in Laravel and set the active state of your menus.

Created on Jul 04, 2021

1011 views

You can get the currently active route in Laravel by making use of the "routeIs()" method of the "request()" helper method. This is the preferred way to check the currently active route as you can reference the "route name" to get the URL value instead of manually typing the URL to check if the current route is active.

Route Definition
In the route definition, provide the route name to allow it to be referred to.
Route::get('/home', [\App\Http\Controllers\HomeController::class, 'index'])
    ->name('home.index');

Checking Active Route
When checking to see the active route, pass on the "route name" to the "routeIs()" method.
<a
    href="{{ route('home.index') }}"
    class="link {{ request()->routeIs('home.index') ? 'link-active' : null }}"
>
    Home
</a>

Now anytime the route is updated for example from "/home" to "/homepage", nothing should be changed since the "route name" is used to reference the URL.

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

Load comments for How to Get Currently Active Route in Laravel

)