How to Get Current Route Name in Laravel - TechvBlogs

How to Get Current Route Name in Laravel

In this short article, You will learn How to Get the Current Route Name in Laravel.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

2 months ago

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

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

Comments (0)

ServerAvatar
Multiple sites on a single VPS/VM/Server Deploy PHP sites on cloud in minutes, simple as that!
ServerAvatar
Generate Free SSL Certificate - SSLForWeb

Generate Free SSL Certificate

  • 90-Day Certificates
  • SSL Monitoring
  • Wildcard Certificates
  • Multiple Domains
  • Certificate Management
  • Expiration Reminders

Comment


Note: All Input Fields are required.