首页 > 解决方案 > Laravel 从给定的 URL 获取路由

问题描述

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

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

标签: phplaravelurl-routing

解决方案


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))

推荐阅读