Home >PHP Framework >Laravel >Detailed explanation of how to configure 404 and other exception pages in the laravel framework (code example)
This article brings you a detailed explanation (code example) of how to configure 404 and other exception pages in the laravel framework. It has certain reference value. Friends in need can refer to it. I hope It will help you.
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 time exception is rendered into the http response, we only need to modify the render method.
Many methods on the Internet are to modify 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); }
There may be no problem with your test at this time, but if you write a login method, if you visit a page that requires login, an error will be reported
This is because if you visit a page where you must log in, you will enter the render method of app/Exceptions/Handler.php. At this time, $exception ->getStatusCode() does not exist, and an error will be reported at this time. So how to solve it?
At this time we find the parent::render method:
At this time we find that the laravel framework has already converted our This situation is included, then we can 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); }
This problem is perfectly solved at this time
Then create a new error page under resources/view/error/, error The page is named: {errorcode}..balde.php, where errorcode is the error code, for example 404..balde.php
After the configuration is completed, it will jump to you when accessing a non-existent route. Configured 404 page
The above is the detailed content of Detailed explanation of how to configure 404 and other exception pages in the laravel framework (code example). For more information, please follow other related articles on the PHP Chinese website!