xxxxxxxxxx
A very simple one
php artisan vendor:publish --tag=laravel-errors
xxxxxxxxxx
//Some exceptions describe HTTP error codes from the server. For example, this may be a "page not found" error (404),
//In order to generate such a response from anywhere in your application, you may use the abort helper:
return abort(404);
// OR
return abort(404, "page not found");
xxxxxxxxxx
// Create a file in resources/views/errors/404.blade.php and add this code.
@extends('errors::minimal')
@section('title', __('Not Found'))
@section('code', '404')
@if($exception)
@section('message', $exception->getMessage())
@else
@section('message', __('Not Found'))
@endif
abort(404, 'Whatever you were looking for, look somewhere else');
xxxxxxxxxx
// 1. Create a new view file for the custom 404 page.
// - In this example, let's assume the view file is named '404.blade.php'.
// - Create this file under the 'resources/views/errors' directory.
// 2. Configure the Laravel application to use the custom 404 page.
// - Open the 'app/Exceptions/Handler.php' file.
// - Find the 'render' method and update it as follows:
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
public function render($request, Throwable $exception)
{
if ($exception instanceof NotFoundHttpException && $request->wantsJson()) {
return response()->json(['error' => 'Resource not found'], 404);
}
return parent::render($request, $exception);
}
// 3. Configure Laravel to use the custom view for 404 errors.
// - Open the 'app/Exceptions/Handler.php' file again.
// - Add the following method to the 'Handler' class:
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Http\Response;
protected function renderHttpException(NotFoundHttpException $e)
{
if (view()->exists('errors.404')) {
return response()->view('errors.404', [], 404);
}
return (new SymfonyExceptionHandler(config('app.debug', false)))->render($e);
}
// That's it! Now when a 404 error occurs in your Laravel application,
// it will display the custom 404 page you created.