dongyun8138 2019-07-12 10:50
浏览 304
已采纳

Laravel从给定的URL获取路由

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

  • 写回答

1条回答 默认 最新

  • duanke8011 2019-07-12 11:03
    关注

    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))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?