如何實現404頁面?方法其實有很多種,接下來本篇文章就給大家介紹如何用laravel 5.5+框架更好的來實現404響應,話不多說,讓我們來看一看具體的內容。
Laravel 5.5.10 封裝了兩個有用的路由器方法,可以幫助我們為用戶提供更好的 404 頁面。現在,當拋出 404 異常時,Laravel 會顯示一個漂亮的 404.blade.php 視圖文件,你可以自定義顯示給用戶 UI,但在該視圖中,你無權訪問 session,cookie,身份驗證(auth)等…
在 laravel 5.5.10 中,我們有一個新的 Route::fallback() 方法,用于定義當沒有其他路由與請求匹配時 Laravel 回退的路由。
Route::fallback(function () { return 'Sorry' . auth()->user()->name . '! This page does not exist.'; });
所以,現在我們可以使用具有正常頁面和頁腳的應用布局,來替代簡單的 404 視圖,同時還能給用戶顯示一條友好的提示信息。
Route::fallback(function() { return response()->view('notFound', [], 404); });
@extends('layout.app') @section('content') <h3>Sorry! this page doesn't exist.</h3> @stop
當 Laravel 渲染這個回退(fallback)路由時,會運行所有的中間件,因此當你在 web.php 路由文件中定義了回退路由時,所有處在 web 中間件組的中間件都會被執行,這樣我們就可以獲取 session 數據了。
API 接口說明
現在當你點擊 /non-existing-page 時,你會看到在回退路由中定義的視圖,甚至當你點擊 /api/non-existing-endpoint 時,如果你也不想提供這個接口,你可以到 api 回退路由中定義 JSON 響應,讓我們到 api.php 路由文件中定義另外一個回退路由:
Route::fallback(function() { return response()->json(['message' => 'Not Found!]); });
由于 api 中間件組帶有 /api 前綴,所有帶有 /api 前綴的未定義的路由,都會進入到 api.php 路由文件中的回退路由,而不是 web.php 路由文件中所定義的那個。
使用 abort(404) 和 ModelNotFound 異常
當使用 abort(404) 時會拋出一個 NotFoundHttpException,此時處理器會為我們渲染出 404.blade.php 視圖文件,同樣的 ModelNotFoundException 異常也會做同樣的處理,那么我們應該如何如何處理才能在更好的渲染出回退路由的視圖,而不是一個普通的視圖呢?
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::respondWithRoute(‘fallback’) 回去跑名為 fallback 的路由,我們可以像下面這樣為回退路由命名:
Route::fallback(function() { return response()->view('notFound', [], 404); })->name('fallback');
甚至,你還可以為特定的資源指定回退路由:
if ($exception instanceof ModelNotFoundException) { return $exception->getModel() == Server::class ? Route::respondWithRoute('serverFallback') : Route::respondWithRoute('fallback'); }
現在我們需要在路由文件中定義這個回退路由:
Route::fallback(function(){ return 'We could not find this server, there are other '. auth()->user()->servers()->count() . ' under your account ......'; })->name('serverFallback');
相關推薦: