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

报告相同问题?

悬赏问题

  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 arbotix没有/cmd_vel话题
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据