Home > PHP Framework > Laravel > Detailed explanation of how to configure 404 and other exception pages in the laravel framework (code example)

Detailed explanation of how to configure 404 and other exception pages in the laravel framework (code example)

不言
Release: 2019-01-07 11:09:34
forward
4495 people have browsed it

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);
}
Copy after login

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

Detailed explanation of how to configure 404 and other exception pages in the laravel framework (code example)

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:

Detailed explanation of how to configure 404 and other exception pages in the laravel framework (code example)

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);
}
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
404 - nginx configuration 40 page
From 1970-01-01 08:00:00
0
0
0
css - How to create a 404 page
From 1970-01-01 08:00:00
0
0
0
Unknown 404 error
From 1970-01-01 08:00:00
0
0
0
About Mac MAMP PRO deploy thinkphp5.1 access 404
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template