【问题标题】:Laravel get route from given URLLaravel 从给定的 URL 获取路由
【发布时间】:2019-07-12 10:50:56
【问题描述】:

拉拉维尔。如何从给定的 URL 获取路由。我知道这个答案:https://stackoverflow.com/a/36476224/9696145

这种方式对我没有帮助,因为如果您设置错误的 URI,我的应用程序出于某种原因返回 404 视图。我只需要返回 null 之类的东西

【问题讨论】:

标签: php laravel url-routing


【解决方案1】:

https://laracasts.com/discuss/channels/laravel/how-to-get-a-route-from-uri

try {
    $url = 'url';
    $route = app('router')->getRoutes()->match(app('request')->create($url))
} catch (NotFoundHttpException $e) {
    $route = null;
}

在非获取路线的情况下:

$method = 'POST';
$url = 'url';
$route = app('router')->getRoutes()->match(app('request')->create($url , $method))

【讨论】:

  • @barmaxon 是的。看起来match 的路由集合方法抛出了异常。为什么不能使用 try-catch 来处理呢?请查看匹配方法的代码。我没有 laravel 应用程序,但在文档中有说明:laravel.com/api/5.8/Illuminate/Routing/…
猜你喜欢
  • 2014-08-26
  • 1970-01-01
  • 2016-06-15
  • 2012-01-03
  • 1970-01-01
  • 2019-08-11
  • 2017-10-02
  • 2019-08-01
相关资源
最近更新 更多