All exceptions in Laravel are handled by the Handler class, which contains two methods: Report and Render, where the Render method renders the exception into the HTTP response. Laravel’s Handler class file location: app/Exceptions/Handler. Since the render method is not rendered in the HTTP response, we only need to modify the render method. Most methods on the web are to change the render method to:

public function render($request, Exception $exception)
{
    if ($exception) {
        return response()->view('error.'.$exception->getStatusCode(), [],$exception->getStatusCode());
    }
    return parent::render($request, $exception);
}

Your test may be fine, but if you write a login method, you will get an error if you visit a page that must be logged in

This is because if you visited must login page, then, would be the app/Exceptions/Handler. PHP render method, then $exception – > getStatusCode () doesn’t exist, at that time will be an error, then how to solve?

At this point we find the parent::render method:

At this point, we realized that the Laravel framework had already included our case, so we could change the above method to:

public function render($request, Exception $exception) { if (! ($exception instanceof AuthenticationException)) { return response()->view('error.'.$exception->getStatusCode(), [],$exception->getStatusCode()); } return parent::render($request, $exception); }

Create a new error page under resources/view/error/ and name it {errorcode}.. Balde.php, where the errorcode is an errorcode, such as 404.. balde.php

After the configuration is complete, visit a non-existent route and jump to the 404 page you configured

Author: huaweichenai source: www.wj0511.com, https://www.wj0511.com/site/d… Copyright notice: this article is the original blog article, reprint please attach blog link!