How to get the current route in Laravel

Updated: January 18, 2024 By: Guest Contributor Post a comment

Introduction

When building applications with the Laravel framework, it’s a common need to determine the current route the application is dealing with, especially for functionality that depends on the route’s properties, such as middleware, access control, or simply dynamically setting active states in a navigation bar. Understanding how to retrieve the current route information is fundamental for effective and dynamic Laravel application development.

In Laravel, route information is handled by the Route facade, which provides numerous methods to interact with routes of the application. In this tutorial, we will explore various ways to get the current route in Laravel and utilize it inside your application.

Accessing the Current Route

To begin with, you can access the current route by using the current method provided by the Route facade. Here’s how you can use this:

use Illuminate\Support\Facades\Route;

// Get the current route
$route = Route::current();

This will return an instance of the current route which you can use to extract further information.

Getting the Route Name

Laravel allows routes to be named for convenient reference. To get the name of the current route, you can use the currentRouteName method:

// Get the name of the current route
$routeName = Route::currentRouteName();

If you need to perform a check against the current route’s name within a view or a middleware, you can do it like this:

if (Route::currentRouteName() === 'home') {
    // Do something specific for the home route
}

Getting the URL and Route Parameters

Besides the name, you may also want to retrieve the route’s URL or any parameters passed within the URL. You can do it using the currentRouteAction and current‘s parameter methods respectively:

// Get the full URL to the current route
$currentUrl = url(Route::current()->uri());

// Get the current route parameters
$routeParameters = Route::current()->parameters();

These parameters are the ones you would generally define within the route’s URL, for example:

Route::get('/user/{id}', function ($id) {
    // Your logic here
})->name('user.show');

In the above route, there is a parameter {id} that you can retrieve using the parameters method.

Using Route Facade’s Helper Functions

Laravel offers several helper functions that you can use directly, instead of calling the methods on the Route facade. Here are a few examples:

// Using the helper function to get the current route
$route = request()->route();

// Getting the current route name
$routeName = request()->route()->getName();

// Getting current route action
$routeAction = request()->route()->getActionName();

// Getting route parameters values
$userId = request()->route('id');

The request() function provides a convenient way to access the current request information, including the route.

Conditional Checks based on Route Patterns

Sometimes, you may want to perform actions based on a pattern that the route’s name follows. You can use the is method for this purpose:

// Check if the route name matches a pattern
if (Route::is('admin.*')) {
    // The route name starts with 'admin.'
}

This can be particularly useful in views to set active states depending on a section of the site.

Middlewares and Route Information

In middleware, accessing the current route is a common task. Middleware methods have access to the $request object, which in turn can give you the route:

public function handle($request, Closure $next)
{
    $currentRoute = $request->route();
    // Your logic here

    return $next($request);
}

This allows you to perform verifications or modifications based on the route within your middleware.

Displaying Route Information in Views

For displaying the current route information in views for navigational elements or breadcrumbs, you can directly use the Route facade:

<ul>
    <li class="{{ Route::is('home') ? 'active' : '' }}">Home</li>
    <li class="{{ Route::is('about') ? 'active' : '' }}">About</li>
    <li class="{{ Route::is('contact') ? 'active' : '' }}">Contact</li>
</ul>

Checking the active route can be leveraged to create a dynamic user experience within the application’s UI.

Conclusion

Understanding how to obtain the current route and its attributes in Laravel is vital for building dynamic and responsive applications. This tutorial covered various methods provided by the Laravel framework to access and manipulate route information to suit your application needs. By leveraging Laravel’s powerful routing capabilities, including named routes and middleware, developers can control application flow and logic more efficiently.

Now that you’ve learned how to get the current route information, experiment with these methods within your Laravel application to optimize your controllers, views, and middleware. It will help your application become more aware and responsive to the actions that the users are performing.