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

Patricia Arquette
發布: 2024-10-20 12:18:02
原創
872 人瀏覽過

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

在Laravel (v5 - v7) 中取得目前路由名稱

在早期版本的Laravel (v4) 中, Route::currentRoute ( ) 方法可用來擷取目前的路線名稱。然而,從 Laravel v5 開始,這個方法不再可用。

Laravel v5

在Laravel v5 中,你可以使用以下方法取得目前的路由名稱:

  • 路線::getCurrentRoute::getCurrentRoute( )->getPath()
  • 請求::route()->getName()
  • 路線::getFacadeRoot( )->current()->uri() (從v5.1開始)

Laravel v5.2

從Laravel v5.2 開始,重新引入Route::currentRouteName() 方法:

  • Route::currentRouteName()(使用IlluminateSupportFacadesRoute)

Laravel v5.3 - v5。 8

在Laravel v5.3 及更高版本:

  • $name = Route::currentRouteName()

Laravel v6 .x - v7.x
  • 此方法在Laravel v6.x 和v7.x 中保持不變:

$name = Route::currentRouteName ()

使用Request
  • 在Laravel 的所有版本中,你也可以使用Request 物件來取得目前的路由名稱:

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

  • 其他資訊
在Laravel 5.3 中,您也可以使用$route 物件取得目前路由名稱、操作和其他詳細資訊。 從 Laravel v6.x 開始,路由文件建議使用 Route::currentRouteName() 方法來取得目前路由名稱.

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

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