在Laravel 5中,App::missingApp::error是不可用的,那么现在如何捕捉异常和丢失的页面呢?

我在文件中找不到任何与此相关的信息.

推荐答案

在Laravel 5中,可以通过编辑app/Exceptions/Handler.php中的render方法捕捉异常.

If you want to catch a missing page (also known as NotFoundException) you would want to check if the exception, $e, is an instance of Symfony\Component\HttpKernel\Exception\NotFoundHttpException.

public function render($request, Exception $e) {
    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
        return response(view('error.404'), 404);

    return parent::render($request, $e);
}

With the code above, we check if $e is an instanceof of Symfony\Component\HttpKernel\Exception\NotFoundHttpException and if it is we send a response with the view file error.404 as content with the HTTP status code 404.

This can be used to ANY exception. So if your app is sending out an exception of App\Exceptions\MyOwnException, you check for that instance instead.

public function render($request, Exception $e) {
    if ($e instanceof \App\Exceptions\MyOwnException)
        return ''; // Do something if this exception is thrown here.

    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
        return response(view('error.404'), 404);

    return parent::render($request, $e);
}

Laravel相关问答推荐

Hostinger Laravel网站未显示错误

如何提取每组 groupBy 查询的唯一值?

使用 laravel 根据当月显示注册用户列表

Laravel Homestead vagrant up 超时

Laravel 集合排序不生效

Laravel 5.4 存储:下载文件.文件不存在,但显然存在

Laravel 5.5 在迁移文件中设置整数字段的大小

带有消息...必须返回关系实例的 LogicException.

如何卸载 Laravel?

Laravel 动作未定义

如何在 Laravel 中实现数组类型路由?

如何从 laravel 中的现有数据库创建迁移

WhereNotExists Laravel Eloquent

Laravel 5 Mime 验证

Laravel Mail 发送Electron邮件但返回 false

Elastic search全文与mysql全文?

Laravel 在域和子域中共享 cookie 检测问题

Laravel 单元测试依赖注入

laravel - 无法在控制器构造函数中获取会话

如何在 Laravel 5 请求类中使用有时规则