Home >Backend Development >PHP Tutorial >How to Get the Current Route Name in Laravel (v5 - v7)?

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

Patricia Arquette
Patricia ArquetteOriginal
2024-10-20 12:18:02949browse

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 IlluminateSupportFacadesRoute)

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.

The above is the detailed content of How to Get the Current Route Name in Laravel (v5 - v7)?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn