Home / Snippets / How to Get Currently Active Route in Laravel
How to Get Currently Active Route in Laravel cover

How to Get Currently Active Route in Laravel

1.1K

3 years ago

0 comments

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.
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this