Frame approach laravel exception page 404. Detailed configuration (sample code)

The contents of this article is to bring about laravel frame configuration 404 and other abnormalities of the page Detailed methods (code examples), there is some reference value, a friend in need can refer to, I hope for your help.

Exception Handler class all by Laravel the process, which contains two methods: report and the render, wherein the abnormality render to render method http response. Handler class file location laravel of: app / Exceptions / Handler, due to the render method to render abnormal time http response, so we only need to modify the method to render under
a lot of online method is modified to render method:

1

2

3

4

5

6

7

public function render($request, Exception $exception)

{

    if ($exception) {

        return response()->view('error.'.$exception->getStatusCode(), [],$exception->getStatusCode());

    }

    return parent::render($request, $exception);

}

This time you test might be no problem, but if you if you write a method to log it, if this time when you have to log in to access the page, this time will complain

This is due to the time if you visit a page must be logged in, and this time will enter the app / Exceptions / Handler.php render method, this time $ exception-> getStatusCode () does not exist, this time it will error , then how to solve it?

This time we find a parent :: render method where:

This time we found out that our laravel framework has been included into this situation, and then we can read about to the above method:

1

2

3

4

5

6

7

public function render($request, Exception $exception)

{

    if (!($exception instanceof AuthenticationException)) {

        return response()->view('error.'.$exception->getStatusCode(), [],$exception->getStatusCode());

    }

    return parent::render($request, $exception);

}

This time it is the perfect solution to this problem
and create a new error page in resources / view / error / following, named error page is: {errorcode} .. balde.php, which errorcode error code, for example 404..balde.php

Once configured to jump to page 404 when you configure the access route that does not exist

Laravel frame is arranged above exception page 404. Detailed methods (sample code) details

Guess you like

Origin www.cnblogs.com/it-3327/p/11782692.html