dqpkea9486 2016-05-26 14:12
浏览 238
已采纳

Laravel中未找到自定义视图异常

Is there a way to capture the view not found exception (not the http route not found) in Laravel 5.2 and display a custom error? I tried to capture InvalidArgumentException in app/Exceptions/handler and return a simple message with no luck.

Thank you in advance

  • 写回答

1条回答 默认 最新

  • dpir3950 2016-05-26 15:27
    关注

    You can include this in your app\Exception\Handler.php file's render method:

    if($e instanceof InvalidArgumentException) abort(404);
    

    Don't forget to use InvalidArgumentException; at the top of class.

    EDIT: I can see this is a general Exception and lots of classes throwing it. So when some other class throws this Exception, you will still get 404.

    So just a quick hack:

    if(($e instanceof InvalidArgumentException) && str_contains($e->getFile(), 'Illuminate/View/FileViewFinder')) abort(404);
    

    So this will only show 404 when FileViewFinder file throws InvalidArgumentException.

    P.S May be there are better solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法