Laravel get route name from given URL(Laravel 从给定的 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')) 这将返回 IlluminateRoutingRoute 实例,您可以在其中调用多个有用的公共方法,例如 getAction、getValidators等检查源https://github.com/illuminate/routing/blob/master/Route.php 了解更多详情.
Also when you run app('router')->getRoutes()->match(app('request')->create('/qqq/posts/68/u1', 'POST')) this will return IlluminateRoutingRoute 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 获取路由名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 从给定的 URL 获取路由名称
基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
