When laravel5.2 accesses a non-existent route, how does one jump to the 404 and LNMP environment? How does one configure it? When laravel5.2 accesses a non-existent route, how does one jump to the 404 and LNMP environment? How does one configure it?
Reply content:
When laravel5.2 accesses a non-existent route, how does one jump to the 404 and LNMP environment? How does one configure it?
TheoreticallydebugWhen it is off, the online environment will automatically arrive404.
Do you want to "go to Page 404" or "show page 404 」? To Redirect, configure app/Exceptions/handler. php and return a Redirect response when NotFoundException is thrown.
1. if you only want to throw a 404 error, the debug switch can satisfy you. 2. if you want to handle exceptions or custom exceptions, refer to the following;
In the root directory of the laravel projectappUnderExceptionsDirectoryHandler.phpFile; here we canCustom exceptionAndException handling;
The most common one isModelNotFoundException
The following is a Demo:
Route:
vikin.cc/article/8
Handler file:
// Handle Http response exceptions public function render ($ request, Exception $ e) {switch ($ e) {// use the type operator instanceof to determine an Exception (instance) modelNotFoundException case ($ e instanceof ModelNotFoundException): // handle exceptions return $ this-> renderException ($ e); break; default: return parent: render ($ request, $ e) ;}// handle exceptions protected function renderException ($ e) {switch ($ e) {case ($ e instanceof ModelNotFoundException): // custom exception, here we return a 404 page return view ('errors. 404 '); break; default: // If The exception is not ModelNotFoundException, return the laravel default error page return (new SymfonyDisplayer (config ('app. debug ')-> createResponse ($ e );}}
Through the above cases, you can easily handle exceptions and give users a friendly prompt!