Invokable (Single-Action) Controllers
Invokable controllers contain a single __invoke() method for standalone actions that don't fit CRUD resources. Use them for dashboards, report generation, webhook handlers, password resets, and any other operation that represents a single, specific action. They keep single-purpose logic neatly encapsulated without the overhead of a full resource controller.
Invokable Controller
// php artisan make:controller ShowDashboard --invokable
class ShowDashboard extends Controller
{
public function __invoke(): View
{
return view('dashboard', [
'users' => User::count(),
'posts' => Post::published()->count(),
'revenue' => Order::thisMonth()->sum('total'),
'recentOrders' => Order::latest()->take(5)->get(),
]);
}
}
// Route — no method name needed
Route::get('/dashboard', ShowDashboard::class)
->middleware('auth')
->name('dashboard');
// Other good use cases for invokable controllers:
Route::post('/webhooks/stripe', HandleStripeWebhook::class);
Route::get('/reports/sales', GenerateSalesReport::class)->middleware('auth:admin');
Route::post('/password/reset', ResetUserPassword::class);
Route::post('/posts/{post}/publish', PublishPost::class)->middleware('auth');
// Each action is a self-contained class — easy to test, easy to findWhen to Use Invokable vs Resource Controllers
- Use resource controllers for standard CRUD operations on models: PostController, UserController, ProductController
- Use invokable controllers for actions that don't fit CRUD: publishing a post (PublishPost), generating a report (GenerateInvoice), handling a webhook (HandlePaymentWebhook)
- Invokable controllers make test organization trivial — one test file per action class is immediately intuitive
- From a SOLID principles perspective, invokable controllers follow the Single Responsibility Principle perfectly — each class does exactly one thing
- You can still apply middleware, policies, and validation inside __invoke() — they're not second-class citizens, just single-method controllers
Tip
Tip
Practice Invokable SingleAction Controllers in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Middleware = filters that run before/after your controller logic
Practice Task
Note
Practice Task — (1) Write a working example of Invokable SingleAction Controllers from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Invokable SingleAction Controllers is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.
Key Takeaways
- Invokable controllers contain a single __invoke() method for standalone actions that don't fit CRUD resources.
- Use resource controllers for standard CRUD operations on models: PostController, UserController, ProductController
- Use invokable controllers for actions that don't fit CRUD: publishing a post (PublishPost), generating a report (GenerateInvoice), handling a webhook (HandlePaymentWebhook)
- Invokable controllers make test organization trivial — one test file per action class is immediately intuitive