How to Get Current Route Name in Laravel

Avatar
C

Callum Davies

Senior Backend Engineer••
8136Views
5mRead
0Reactions

In this comprehensive tutorial, you'll master the art of retrieving the current route name using Laravel versions 6 through 10. Unveil multiple strategies to effortlessly fetch the route name within controllers, middleware, or blade files. Let's embark on this journey:

Get route name in Controller or Middleware

1. You can get the route name using the route facade in the controller or middleware

use Illuminate\Support\Facades\Route;



public function index(){
        $routeName = Route::currentRouteName();
	    dd($routeName);
    }

2. Using request facade to get the current route name

public function index(){
        $routeName = \Request::route()->getName();
        dd($routeName);
    }

Get the route name in Blade View

Similarly, you can also use in blade file to get the current route

{{ Route::currentRouteName() }}

Thanks for your time, hope it will help you to getting the route name in the controller, middleware or in the view where you want.

Connect with KoderSolution 🚀

Master modern software engineering, scalable architectures, and advanced backend systems.

What's your setup?How does your production stack handle background jobs? Are you using Redis with Horizon, or cloud queues like AWS SQS?
Callum Davies
C
Written by•Senior Backend Engineer

Callum Davies

PHP & Laravel practitioner since v5.4. Big believer in thin controllers, explicit return types, and boring modular architecture that doesn't page you on weekends.

React to this article

Recommended Articles

View all articles