惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
D
Docker
WordPress大学
WordPress大学
罗磊的独立博客
J
Java Code Geeks
博客园 - 【当耐特】
博客园 - 司徒正美
雷峰网
雷峰网
H
Help Net Security
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
T
Tailwind CSS Blog
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
B
Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Laravel HTTP Error Custom Views (404, 403, 500, etc.)
Rafli Zocky · 2026-05-11 · via DEV Community
Cover image for Laravel HTTP Error Custom Views (404, 403, 500, etc.)

Rafli Zocky

How Laravel Handles HTTP Errors

  1. A request hits a non-existent route
  2. Router throws NotFoundHttpException
  3. Exception is caught by Handler
  4. Laravel looks for resources/views/errors/404.blade.php (example)
  5. Returns an HTTP 404 response with the rendered view

Common Handling

  • app/Exceptions/Handler.php : converts exceptions into HTTP responses.
<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Auth\Access\AuthorizationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * The list of the inputs that are never flashed to the session on validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {
        $this->reportable(function (Throwable $e) {
            //
        });

        // 403 — Forbidden
        $this->renderable(function (AuthorizationException $e, $request) {
            if (!$request->expectsJson()) {
                return response()->view('errors.403', [], 403);
            }
        });

        // 404 — Not Found
        $this->renderable(function (NotFoundHttpException $e, $request) {
            if (!$request->expectsJson()) {
                return response()->view('errors.404', [], 404);
            }
        });

        // 419 — Page Expired (CSRF Token Mismatch)
        $this->renderable(function (TokenMismatchException $e, $request) {
            if (!$request->expectsJson()) {
                return response()->view('errors.419', [], 419);
            }
        });

        // 429 — Too Many Requests
        $this->renderable(function (ThrottleRequestsException $e, $request) {
            if (!$request->expectsJson()) {
                $retryAfter = $e->getHeaders()['Retry-After'] ?? 60;
                return response()->view('errors.429', ['retryAfter' => $retryAfter], 429);
            }
        });

        // 503 — Service Unavailable
        $this->renderable(function (ServiceUnavailableHttpException $e, $request) {
            if (!$request->expectsJson()) {
                return response()->view('errors.503', ['exception' => $e], 503);
            }
        });
    }

    public function render($request, Throwable $exception)
    {
        return parent::render($request, $exception);
    }
}

Enter fullscreen mode Exit fullscreen mode

  • resources/views/errors/{status_code}.blade.php
# example 
# resources\views\errors\404.blade.php

@extends('errors.layout')

@section('error_code', '404')

@section('error_content')
    <div class="text-center position-relative z-index-1" style="max-width: 520px;">

        {{-- Illustration --}}
        <div class="mb-6">
            <img src="{{ asset('assets/media/illustrations/unitedpalms-1/18.png') }}"
                 class="error-illustration theme-light-show" alt="404 Not Found" />
            <img src="{{ asset('assets/media/illustrations/unitedpalms-1/18-dark.png') }}"
                 class="error-illustration theme-dark-show" alt="404 Not Found" />
        </div>

        {{-- Title --}}
        <h1 class="fw-bold fs-2x text-gray-900 mb-3">Page Not Found</h1>

        {{-- Description --}}
        <p class="text-muted fs-5 mb-8 fw-semibold">
            Oops! The page you're looking for doesn't exist.<br>
            It may have been moved, deleted, or never existed.
        </p>

        {{-- Actions --}}
        <div class="error-actions d-flex gap-3 justify-content-center flex-wrap">
            <a href="{{ url()->previous() !== url()->current() ? url()->previous() : '/' }}"
               class="btn btn-light btn-lg fw-bold px-8">
                <i class="ki-duotone ki-arrow-left fs-2 me-2">
                    <span class="path1"></span><span class="path2"></span>
                </i>
                Go Back
            </a>
        </div>
    </div>
@endsection

Enter fullscreen mode Exit fullscreen mode

Then, just create simple routes to test it.

Route::get('/test-404', function () {
    abort(404);
});

Enter fullscreen mode Exit fullscreen mode

Also we can customize error pages by creating the views manually or publishing Laravel’s default templates:

php artisan vendor:publish — tag=laravel-errors

Enter fullscreen mode Exit fullscreen mode

Need help building your app? I’m available for freelance web & Android development — raflizocky.netlify.app

☕ Support my writing: paypal.me/raflizocky · saweria.co/raflizocky