Home  >  Article  >  Backend Development  >  How to Get the Current Route Name in Laravel?

How to Get the Current Route Name in Laravel?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-20 12:19:30638browse

How to Get the Current Route Name in Laravel?

How to Retrieve Current Route Name in Laravel (v5 - v7)

In previous versions of Laravel, accessing the current route name was straightforward using Route::currentRouteName(). However, in Laravel v5 and higher, this approach has evolved.

Here's how to get the current route name in:

Laravel v5

You can use the following methods:

  • Route::getCurrentRoute()->getPath();
  • Request::route()->getName() (Available since v5.1)

Laravel v5.2

Reintroduce the Route::currentRouteName(); method (use IlluminateSupportFacadesRoute;) for convenience.

Laravel v5.3 - v5.8

Extract the route information directly:

  • $route = Route::current();
  • $name = Route::currentRouteName();
  • $action = Route::currentRouteAction();

Laravel v6.x - v7.x

Similar to Laravel v5.3 - v5.8:

  • $route = Route::current();
  • $name = Route::currentRouteName();
  • $action = Route::currentRouteAction();

Additional Notes:

  • Laravel 5.2 documentation provides detailed information on Route::current() and Route::getCurrentRouteAction(): https://laravel.com/docs/5.2/routing#retrieving-the-current-route
  • Laravel 5.3 documentation explains Route::currentRouteName() and Route::currentRateAction(): https://laravel.com/docs/5.3/routing#accessing-the-route-instance-and-related-information
  • Laravel 6.x documentation maintains the same methods: https://laravel.com/docs/6.x/routing#retrieving-the-current-route-and-parameters
  • The request class also provides an alternative for retrieving the route name: $request->route()->getName();

The above is the detailed content of How to Get the Current Route Name in Laravel?. 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