Laravel从给定的URL获取路由名称 [英] Laravel get route name from given URL
问题描述
在Laravel中,我们可以通过以下方法从当前URL获取路由名称:
In Laravel, we can get route name from current URL via this:
Route::currentRouteName()
但是,如何从特定的给定URL获取路由名称?
But, how can we get the route name from a specific given URL?
谢谢.
推荐答案
一种非常简单的方法 Laravel 5.2
A very easy way to do it Laravel 5.2
app('router')->getRoutes()->match(app('request')->create('/qqq/posts/68/u1'))->getName()
它输出我的路线名称,例如 slug.posts.show
It outputs my Route name like this slug.posts.show
更新:对于 POST , PUT 或 Delete 之类的方法,您可以这样做
Update: For method like POST, PUT or DELETE you can do like this
app('router')->getRoutes()->match(app('request')->create('/qqq/posts/68/u1', 'POST'))->getName()//reference https://github.com/symfony/http-foundation/blob/master/Request.php#L309
同样,当您运行app('router')->getRoutes()->match(app('request')->create('/qqq/posts/68/u1', 'POST'))
时,这将返回Illuminate\Routing\Route
实例,您可以在其中调用多个有用的公共方法,例如getAction
,getValidators
等.请检查源
Also when you run app('router')->getRoutes()->match(app('request')->create('/qqq/posts/68/u1', 'POST'))
this will return Illuminate\Routing\Route
instance where you can call multiple useful public methods like getAction
, getValidators
etc. Check the source https://github.com/illuminate/routing/blob/master/Route.php for more details.
这篇关于Laravel从给定的URL获取路由名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!