"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Get the Current Route Name in Laravel (v5 - v7)?

How to Get the Current Route Name in Laravel (v5 - v7)?

Published on 2024-11-03
Browse:803

How to Get the Current Route Name in Laravel (v5 - v7)?

Get Current Route Name in Laravel (v5 - v7)

In earlier versions of Laravel (v4), the Route::currentRouteName() method could be used to retrieve the current route name. However, from Laravel v5 onwards, this method is no longer available.

Laravel v5

In Laravel v5, you can get the current route name using the following methods:

  • Route::getCurrentRoute()->getPath()
  • \Request::route()->getName()
  • Route::getFacadeRoot()->current()->uri() (from v5.1)

Laravel v5.2

From Laravel v5.2 onwards, the Route::currentRouteName() method is reintroduced:

  • Route::currentRouteName() (use Illuminate\Support\Facades\Route)

Laravel v5.3 - v5.8

In Laravel v5.3 and later:

  • $name = Route::currentRouteName()

Laravel v6.x - v7.x

This method remains the same in Laravel v6.x and v7.x:

  • $name = Route::currentRouteName()

Using Request

In all versions of Laravel, you can also use the Request object to get the current route name:

  • $name = $request->route()->getName()

Additional Information

  • In Laravel 5.3, you can also use the $route object to get the current route name, action, and other details.
  • From Laravel v6.x onwards, the route documentation recommends using the Route::currentRouteName() method for getting the current route name.
Release Statement This article is reprinted at: 1729397176 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3