php - Laravel 从给定的 URL 获取路由名称

标签 php laravel routing

在 Laravel 中,我们可以通过以下方式从当前 URL 获取路由名称:

Route::currentRouteName()

但是,我们如何从特定的给定 URL 中获取路由名称?

谢谢。

最佳答案

一个非常简单的方法 Laravel 5.2

app('router')->getRoutes()->match(app('request')->create('/qqq/posts/68/u1'))->getName()

它像这样输出我的路线名称 slug.posts.show

更新:对于POSTPUTDELETE 等方法,您可以这样做

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)方法,如 getActiongetValidators 等。检查来源https://github.com/illuminate/routing/blob/master/Route.php了解更多详情。

关于php - Laravel 从给定的 URL 获取路由名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24582922/

相关文章:

javascript - 文本字符串值发布到另一个页面的输入字段

php - 调试和检查 $_POST

php - Laravel 如何延迟在单个服务提供者中列出的多个绑定(bind)?

linux - laravel 4 关于新安装的建议

python - 在 Python 中通过 NAT 中继/发送

php - 使用 Silex 的灵事件态路由

angularjs - Angular UI 路由器 - 强制 URL 参数?

javascript - 使用 html 表单将 javascript 变量值传递给 .php

php图像和文本上传不起作用

laravel - vue 2.0 组件挂载失败 : template or render function not defined