-3

Laravel. How to get route from given URL. I know about this answer: https://stackoverflow.com/a/36476224/9696145

This way doesn't help me bacause if you set wrong URI my app for some reason return 404 view. I need to just return something like null

barmaxon
  • 199
  • 1
  • 15

1 Answers1

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;
}

In case of non-get routes:

$method = 'POST';
$url = 'url';
$route = app('router')->getRoutes()->match(app('request')->create($url , $method))
  • @barmaxon Yep. Looks like `match` method of route collection throws an exception. Why can't you use try-catch to handle it? Please look to the code of match method. I don't have laravel application but it's explained in the docs: https://laravel.com/api/5.8/Illuminate/Routing/RouteCollection.html#method_match – Viktar Pryshchepa Jul 12 '19 at 11:30