☝️ Invokable Controllers in Laravel
An Invokable Controller is a special type of controller with only one method: __invoke(). It's perfect for single-action tasks like forms, pings, or one-off views.
🧐 When to Use?
- Pages like
/contact,/about - AJAX-only endpoints with one job
- Webhook handlers, email verifications, etc.
📦 Create an Invokable Controller
Use this Artisan command:
php artisan make:controller ContactController --invokable
Laravel creates: app/Http/Controllers/ContactController.php
✏️ Inside the Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ContactController extends Controller
{
public function __invoke(Request $request)
{
return view('contact');
}
}
The __invoke() method is automatically called when the route matches.
🔗 Define Route for It
Because it only has one method, you don’t need to specify it:
use App\Http\Controllers\ContactController;
Route::get('/contact', ContactController::class);
This automatically triggers the __invoke() method.
🔐 Add Middleware
Route::get('/secure-contact', ContactController::class)
->middleware('auth');
🔢 With Parameters
Route::get('/message/{id}', MessageController::class);
public function __invoke($id) {
return "Message ID: $id";
}


