在 Laravel 中,我们可以通过以下方式从当前 URL 获取路由名称:
Route::currentRouteName()
但是,我们如何从特定的给定 URL 中获取路由名称?
谢谢。
最佳答案
一个非常简单的方法 Laravel 5.2
app('router')->getRoutes()->match(app('request')->create('/qqq/posts/68/u1'))->getName()
它像这样输出我的路线名称 slug.posts.show
更新:对于POST、PUT 或DELETE 等方法,您可以这样做
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
实例,您可以在其中调用多个有用的公共(public)方法,如 getAction
、getValidators
等。检查来源https://github.com/illuminate/routing/blob/master/Route.php了解更多详情。
关于php - Laravel 从给定的 URL 获取路由名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24582922/