duanjia7912 2017-08-18 13:01
浏览 222
已采纳

在Laravel 5.4的web.php文件中捕获NotFoundHttpException并抛出自定义异常

I'm trying to find a way of customising the error handling of Laravel 5.4 when a route can't be found. In my web.php there is a route incorrectly defined (deliberately for testing purposes). I have wrapped it in a try...catch block and thrown my own custom exception RoutesException:

try {
    Route::get('terms_rop_labels/view', 'LRChildController@view');
}catch (NotFoundHttpException $ex) {
    throw new RoutesException('terms_rop_labels/view');
}

Then in app\Exceptions\Handler.php I am trying to catch the exception in a test view:

if ($exception instanceof NotFoundHttpException) {
$parameters = [
'message'=> 'NotFoundHttpException'
];
return response()->view('errors.test', $parameters, 500);
}
if ($exception instanceof RoutesException) {
        $parameters = [
            'message'=> 'RoutesException'
        ];
        return response()->view('errors.test', $parameters, 500);
}

Can anyone explain why the handler catches a NotFoundHttpException and not my custom RoutesException?

  • 写回答

1条回答

  • doupin2013 2017-08-18 14:18
    关注

    The route in web.php is not throwing a NotFoundHttpException exception. You are just registering routes in web.php, not resolving them.

    The Route facade in web.php is giving you static access to the get method in the Illuminate\Routing\Router class (see lines of 125 - 135 in https://github.com/laravel/framework/blob/5.4/src/Illuminate/Routing/Router.php)

    /**
     * Register a new GET route with the router.
     *
     * @param  string  $uri
     * @param  \Closure|array|string|null  $action
     * @return \Illuminate\Routing\Route
     */
    public function get($uri, $action = null)
    {
        return $this->addRoute(['GET', 'HEAD'], $uri, $action);
    }
    

    (So you are just adding routes to the RouteCollection with Router get method in the web.php file.)

    If you look in the back-trace, you can see where the NotFoundHttpException exception is being thrown in your case. For example, if you were to navigate to a nonexistent route that you have not added to the route collection by registering it in web.php, you would see that the NotFoundHttpException is being thrown by the RouteCollection class match method on line 179.

    In your case, the try/catch in not catching a NotFoundHttpException because the

    Route::get('terms_rop_labels/view', 'LRChildController@view');
    

    is not throwing the NotFoundHttpException.

    Maybe you can achieve what you want by just catching NotFoundHttpException in the app\Exceptions\Handler instead.

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

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用