首頁  >  文章  >  後端開發  >  如何在 Laravel (v5 - v7) 中取得目前路由名稱?

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

Patricia Arquette
Patricia Arquette原創
2024-10-20 12:18:02875瀏覽

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn