How to Get Current Route Name in Laravel - Techvblogs

How to Get Current Route Name in Laravel

Discover how to get current route name in Laravel easily for dynamic navigation and advanced routing features.


Suresh Ramani - Author - Techvblogs
Suresh Ramani
 

2 days ago

TechvBlogs - Google News

In this short article, We will learn How to Get the Current Route Name in Laravel. We will get the route name in Controller, Middleware, and Blade File. There are some methods in Laravel to get the current route name.

Laravel allows you to define the route and also you can define the name of the route. You can use that route name wherever you need like controller, blade file, etc. You need to use the route() function and pass the route name as an argument in that function. It will return the route using that route name. It's a very easy and helpful method to get and set a route.

Get route name in Blade View

In some cases, you need to display the route name in the Blade files like the active nav link and some other requirements.

{{ Route::currentRouteName() }}

// or

{{ request()->route()->getName() }}

Get route name in Laravel Controller

You can get the route name in Laravel Controller. You can see the following code for the get current route name in Laravel Controller.

public function getRouteName(Request $request){
    $routeName = $request->route()->getName();
    // or
    $routeName = request()->route()->getName();

    return $routeName;
}

Get route name in Laravel Middleware

public function handle($request, Closure $next) {
    $routeName = $request->route()->getRouteName();
    echo $routeName;
}

I hope in this short article you will learn How to Get the Current Route name in laravel.

Thank you for reading this blog.

Read Also: How to Convert JSON to Array in Laravel

Comments (0)

Comment


Note: All Input Fields are required.