我想使用 handle.php 將所有 404 錯誤重定向到主頁,我使用 laravel 8 請幫助我。
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
class Handler extends ExceptionHandler
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->renderable(function (NotFoundHttpException $e, $request) {
});
}
uj5u.com熱心網友回復:
您可以將重定向回傳到主頁:
$this->renderable(function(NotFoundHttpException $ex, $request) {
return redirect('/');
})
但它會產生一個302狀態代碼而不是404但是如果你想產生一個404但仍然顯示主頁。
$this->renderable(function(NotFoundHttpException $ex, $request) {
return response()->view('homepage', [], 404);
})
錯誤處理 - 渲染例外
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/537376.html
