How to Get Current Route Name in Laravel

How to Get Current Route Name in Laravel

In this post we will give you information about How to Get Current Route Name in Laravel. Hear we will give you detail about How to Get Current Route Name in Laravel And how to use it also give you demo for it if it is necessary.

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.

Also see: How to Convert JSON to Array in Laravel

  .       .

Hope this code and post will helped you for implement How to Get Current Route Name in Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US