How to Create Custom Middleware in Laravel 11
Laravel 11 features invokable controllers, which provide a more concentrated way to managing individual actions within your application. This guide explains how to build and use these controllers, stressing their benefits and providing a practical example.
Advantages of Invokable Controllers:
- Enhanced Readability: With a single __invoke method, the controller's purpose is clearly defined, facilitating code maintainability and comprehension.
- Reduced Complexity: By requiring less code, maintenance becomes easier, and the development process becomes more streamlined.
- Concise Routing: Directly referencing the controller class in Laravel 11 routes without specifying a method simplifies route definitions.
Creating an Invokable Controller in Laravel 11:
Utilize the Artisan Command:
- Open your terminal and navigate to the root directory of your Laravel 11 project.
- Execute the following command, replacing ExampleControllerName with your desired name:
bash php artisan make:controller ExampleControllerName --invokable
This command generates a new controller class with the __invoke method, ready for customization.
Structure the __invoke Method:
- Open the generated controller file (typically located in app/Http/Controllers).
- You'll find the __invoke method already defined, where your controller's logic resides.
- Access the request object using the Request type hint:
php public function __invoke(Request $request) { // Your controller logic here }
Implement the desired functionality within this method, utilizing Laravel features such as repositories, models, and validation as required.
Define Routes:
- In your routes file (usually routes/web.php), create a route corresponding to the desired URL:
php Route::get('/your-route', ExampleControllerName::class);
Replace /your-route with the preferred URI and ExampleControllerName with the actual controller class name.
- In your routes file (usually routes/web.php), create a route corresponding to the desired URL:
Example: Handling a Simple Form Submission with an Invokable Controller ```php // app/Http/Controllers/FormController.php
class FormController { public function __invoke(Request $request) { $request->validate([ 'name' => 'required|string', 'email' => 'required|email', ]);
// Process the form data (e.g., save to database)
return redirect('/success');
}
}
// routes/web.php
Route::post('/submit-form', FormController::class); ```
By following these methods, you can design efficient and maintainable invokable controllers in Laravel 11, resulting in cleaner code and a more pleasant development experience.
- Author: Ahmed Bouchefra Follow @ahmedbouchefra
-
Date:
Related posts
How to use new Dumpable Trait in Laravel 11 Creating Custom Validation Rules in Laravel 11 How to Create Custom Middleware in Laravel 11 How to Create Custom Middleware in Laravel 11 How to Install Laravel 11 Application How to Create Routes in Laravel 10 How to Generate App Key in Laravel 11? How to Add Bootstrap 5 to Laravel 11 Build an app with Laravel 11 and MySQL Livewire 3 refresh component Building a RESTful API CRUD Application with Laravel 11 Should I use Livewire in Laravel? Laravel 11 new features & release date Laravel 10 CRUD Tutorial Laravel 10 CORS Explained in Detail Laravel 7/6 Auth Tutorial: Login/Register/Password Reset Example PHP Laravel 7/6 Tutorial: CRUD Example App with Bootstrap 4 and MySQL Database Laravel 7/6 Database Migrations Tutorial with Admin Roles Example Laravel 7/6 Email Verification Tutorial and Example Laravel 7/6 REST API CRUD Tutorial - Build a CRM [PART 2]: Eloquent Models and Relationships Laravel 6/7 Tutorial & New Features - Creating and Migrating a MySQL Database Laravel 7/6 CORS Tutorial and Example How to Install Vue.JS in Laravel 7/6 By Example How to Install Laravel 7 On Ubuntu, Windows and macOS How to Install Bootstrap 4 in Laravel 7/6 Tutorial and Example