How to better realize 404 response with Laravel 5.5+ framework

01-28-2024

How to realize 404 page? In fact, there are many methods. Next, this article will introduce how to use Laravel 5.5+ framework to better realize 404 response. Not much to say, let's take a look at the specific content.

Laravel 5.5.10 encapsulates two useful router methods, which can help us provide better 404 pages for users. Now, when throwing a 404 exception, Laravel will display a beautiful 404.blade.php view file, which you can customize to display to the user UI, but in this view, you have no right to access session, cookie, authentication (auth) and so on. ...

In laravel 5.5.10, we have a new Route::fallback () method, which is used to define the route that Laravel falls back when no other route matches the request.

Route::fallback(function () { return 'Sorry' . auth()->user()->name . '! This page does not exist.'; });

So, now we can use the application layout with normal pages and footers to replace the simple 404 view, and at the same time, we can display a friendly prompt message to users.

Route::fallback(function() { return response()->view('notFound', [], 404); });

@extends('layout.app') @section('content') Sorry! this page doesn't exist. @stop

When Laravel renders this fallback route, all middleware will run, so when you define the fallback route in the web.php routing file, all middleware in the web middleware group will be executed, so that we can get the session data.

Api Interface Description Now when you click /non-existing-page, you will see the view defined in the fallback route. Even when you click /api/non-existing-endpoint, if you don't want to provide this interface, you can define JSON response in the API fallback route, and let's define another fallback route in the api.php routing file:

Route::fallback(function() { return response()->json(['message' => 'Not Found! ]); });

Because the api middleware group has the /api prefix, all undefined routes with the /api prefix will enter the fallback route in the api.php routing file instead of the one defined in the web.php routing file.

Using abort(404) and ModelNotFoundException When abort(404) is used, a NotFoundHttpException will be thrown, and the processor will render the 404.blade.php view file for us, and the same ModelNotFoundException exception will do the same. So how can we handle it to render a fallback view better than a normal view?

class Handler extends ExceptionHandler { public function render($request, Exception $exception) { if ($exception instanceof NotFoundHttpException) { return Route::responseWithRoute('fallback'); } if ($exception instanceof ModelNotFoundException) { return Route::responseWithRoute('fallback'); } return parent::render($request, $exception); } }

Route:: Respond to Throute ('fallback') Go back and run a route named fallback. We can name the fallback route as follows:

Route::fallback(function() { return response()->view('notFound', [], 404); })->name('fallback');

Even, you can specify a fallback route for a specific resource:

if ($exception instanceof ModelNotFoundException) { return $exception->getModel() == Server::class ? Route::respondWithRoute('serverFallback') : Route::respondWithRoute('fallback'); }

Now we need to define this fallback in the routing file by:

Route::fallback(function(){ return 'We could not find this server, there are other '. auth()->user()->servers()->count() . ' under your account ......'; })->name('serverFallback');

Related recommendations:

Mac mamp ngiux laravel frame reported 404 error.

How to Implement Laravel 5.5 Responsive Interface

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us