如何在 Laravel 中取得目前的路由名稱?

Mary-Kate Olsen
發布: 2024-10-20 12:19:30
原創
535 人瀏覽過

How to Get the Current Route Name in Laravel?

如何在Laravel 擷取目前路由名稱(v5 - v7)

在先前的Laravel 版本中,存取目前路由名稱非常簡單使用Route::currentRouteName()。然而,在 Laravel v5 及更高版本中,這種方法已經演變。

以下是如何取得目前路由名稱:

Laravel v5

可以使用以下方法:

  • Route::getCurrentRoute()->getPath();
  • Request::route()->getName () (自v5.1 起可用)

Laravel v5.2

重新引入Route::currentRouteName();方法(使用IlluminateSupportFacadesRoute;)方便。

Laravel v5.3 - v5.8

直接擷取路線資訊:
  • $route = 路線::current();
  • $name = 路線::currentRouteName();
  • $action = 路線::currentRouteAction();

Laravel v6.x - v7 .x

類似Laravel v5.3 - v5.8:
  • $route = Route::current() ;
  • $name =路線::currentRouteName();
  • $action = 路線::currentRouteAction();

附加說明:

  • Laravel 5.2 文件提供了有關Route::current() 和Route::getCurrentRouteAction() 的詳細資訊:https://laravel.com/docs/5.2/routing#retriving-the-current -route
  • Laravel 5.3 文件解釋Route::currentRouteName() 和Route::currentRateAction():https://laravel.com/docs/5.3/routing#accessing-the-route-instance-and-相關資訊
  • Laravel 6.x 文件維護相同的方法:https://laravel.com/docs/6.x/routing#retriving-the-current-route-and-parameters
  • 請求類別也提供了擷取路由名稱的替代方法:$request->route()->getName();

以上是如何在 Laravel 中取得目前的路由名稱?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!