Laravel 12 Middleware in Bagisto introduces a cleaner and more efficient way to manage HTTP requests, making applications easier to maintain and customize.
Laravel has always provided a powerful middleware system to filter and process HTTP requests before they reach your application’s business logic.
Since Bagisto is built on Laravel 12, these improvements directly benefit developers building eCommerce stores, custom modules, and SaaS applications.
What Is Middleware?
Middleware acts as a bridge between an incoming HTTP request and your application’s controllers.
It allows developers to perform tasks such as authentication, authorization, request validation, localization, and rate limiting.
It also handles security checks before processing the request.
In Bagisto, middleware is responsible for protecting admin routes, managing customer sessions, handling localization, and securing API endpoints.
Every request to the admin panel, storefront, or API passes through one or more middleware layers before reaching the application logic.
A simple middleware example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; class CheckAdmin { public function handle(Request $request, Closure $next) { if (! auth()->check()) { return redirect()->route('admin.session.create'); } return $next($request); } |
This middleware checks whether the user is authenticated before allowing access to protected admin routes.
What’s New in Laravel 12?
One of the biggest improvements in Laravel 12 is the simplified middleware configuration.
Instead of configuring middleware primarily in the app/Http/Kernel.php file, Laravel 12 allows developers to manage middleware directly in bootstrap/app.php.
|
1 2 3 4 5 6 7 8 9 10 11 |
use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Middleware; return Application::configure(basePath: dirname(__DIR__)) ->withMiddleware(function (Middleware $middleware) { $middleware->alias([ 'admin' => \App\Http\Middleware\CheckAdmin::class, ]); }) ->create(); |
This centralized configuration keeps your application cleaner and easier to maintain.
Middleware Aliases
Laravel 12 makes middleware aliases easier to define and maintain.
|
1 2 3 4 5 |
$middleware->alias([ 'admin' => CheckAdmin::class, 'seller' => CheckSeller::class, 'locale' => SetLocale::class, ]); |
Using aliases improves route readability and makes middleware reusable across different modules.
Example:
|
1 2 3 |
Route::middleware(['admin'])->group(function () { Route::get('/dashboard', DashboardController::class); }); |
Middleware Groups
Middleware groups help organize multiple middleware into a single logical group.
|
1 2 3 4 |
$middleware->appendToGroup('admin', [ CheckAdmin::class, SetLocale::class, ]); |
Instead of assigning multiple middleware individually, developers can simply use the group name.
How These Improvements Benefit Bagisto
Cleaner Module Development
Bagisto follows a modular architecture, allowing developers to build extensions without modifying the core codebase.
Laravel 12’s middleware configuration makes registering package middleware much simpler and keeps modules upgrade-safe.
Better Maintainability
Large eCommerce projects often contain dozens of middleware.
Laravel 12 centralizes configuration, making applications easier to understand and reducing maintenance overhead.
Enhanced Security
Middleware remains one of the most important security layers in Bagisto. It protects the admin panel, customer accounts, checkout process, and APIs by handling:
- Authentication
- Authorization
- CSRF Protection
- Rate Limiting
- Localization
- Session Validation
Improved Package Compatibility
Laravel 12 simplifies package integration for Bagisto modules. It also reduces upgrade conflicts and improves maintainability.
Middleware in Everyday Bagisto Development
Middleware plays an important role in many Bagisto features.
Protecting Admin Routes
|
1 2 3 4 5 6 7 8 9 |
Route::middleware(['admin'])->group(function () { Route::get('/admin/orders', function () { // Admin Orders }); }); |
Protecting Customer Routes
|
1 2 3 |
Route::middleware(['customer'])->group(function () { Route::get('/checkout', CheckoutController::class); }); |
Applying Localization
|
1 2 3 |
Route::middleware(['locale'])->group(function () { Route::get('/', HomeController::class); }); |
Protecting APIs
|
1 2 3 |
Route::middleware(['auth:sanctum', 'throttle:60,1'])->group(function () { Route::get('/api/orders', OrderController::class); }); |
These examples demonstrate how middleware keeps controllers focused on business logic while handling request validation before execution.
Custom Middleware Example for Bagisto
Suppose you’re building a Marketplace SaaS extension and want to allow only sellers with an active subscription to access certain features.
Create middleware:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
namespace Webkul\Marketplace\Http\Middleware; use Closure; class CheckSellerSubscription { public function handle($request, Closure $next) { if (! auth()->user()->subscription_active) { return redirect()->route('marketplace.subscription'); } return $next($request); } } |
Register the middleware alias:
|
1 2 3 4 |
$middleware->alias([ 'subscription' => CheckSellerSubscription::class, ]);<code class="language-php"> |
Use it in your routes:
|
1 2 3 |
Route::middleware(['subscription'])->group(function () { Route::get('/seller/products', ProductController::class); }); |
This approach keeps subscription validation separate from your controller, resulting in cleaner and more maintainable code.
Best Practices
When building Bagisto applications with Laravel 12:
- Keep middleware focused on request filtering.
- Avoid expensive database operations whenever possible.
- Create reusable middleware for custom Bagisto packages.
- Use middleware aliases for cleaner route definitions.
- Group related middleware to improve maintainability.
- Reserve business logic for controllers or service classes rather than middleware.
Conclusion
Laravel 12 continues to improve the developer experience by simplifying middleware registration and modernizing application configuration.
For Bagisto developers, these enhancements result in cleaner package development, easier maintenance, stronger security, and better scalability.
Whether you’re building a payment gateway, a Marketplace extension, or a SaaS application, Laravel 12 simplifies middleware management.
By embracing these modern practices, Bagisto developers can build secure and scalable eCommerce solutions that are ready for future Laravel releases.
You can also hire laravel developers to build custom solutions on Laravel. To explore ready-made add-ons, visit the Bagisto extension marketplace.