Laravel Kernel & Middleware Notes-MWALA_LEARN

Objectives: Laravel Kernel & Middleware Notes

Laravel Kernel & Middleware Notes

Laravel Kernel & Middleware Notes

1. What is the Laravel Kernel?

The Kernel in Laravel is like the “traffic controller” of the application. It decides:

  • Which requests should pass through security checks.
  • Which logic should run before a controller handles a request.
  • How responses go back to the user.
Real-life analogy: Imagine a building entrance. The Kernel is the security guard who decides who enters, checks ID, and ensures rules are followed before letting people in.

2. Types of Kernel in Laravel

  • HTTP Kernel: Handles browser/API requests. File: app/Http/Kernel.php
  • Console Kernel: Handles command-line (artisan) requests. File: app/Console/Kernel.php

3. HTTP Kernel Responsibilities

  • Manages middleware for web and API routes.
  • Applies global rules to all requests (like trimming inputs, trusting proxies).
  • Applies route-specific middleware (like auth or admin).
  • Handles sessions, CSRF protection, cookies, and response adjustments.
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
    'api' => [
        'throttle:api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'admin' => \App\Http\Middleware\AdminMiddleware::class,
];
    
Real-life analogy: The web group is like the front desk rules for visitors: check ID, record arrival, and give them a pass. The admin middleware is like an extra security layer for VIP rooms: only authorized people can enter.

4. Why Kernel Errors Happen

If a route references a middleware like admin but it is not registered in the Kernel, Laravel cannot find it and throws:

Target class [admin] does not exist.

Solution: either register the middleware in $routeMiddleware or remove it from the route.

5. Middleware & Security

  • Middleware acts like rules or gates that a request must pass through.
  • Examples of middleware:
    • auth: Only logged-in users can access certain pages.
    • admin: Only users with the admin role can access sensitive pages.
    • throttle: Limits how many requests a user can make in a short time.
Real-life example: Your online banking website: - Regular users can see their account. - Admins can see all accounts. - Middleware ensures no regular user can accidentally access admin functions.

6. Summary

  • Laravel Kernel is the main controller of requests and responses.
  • Middleware are rules applied by the Kernel to secure and manage traffic.
  • Always register your custom middleware (like admin) in the Kernel.
  • Think of Kernel as a building security system and middleware as the rules for different rooms.

Reference Book: N/A

Author name: SIR H.A.Mwala Work email: biasharaboraofficials@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::