Single action controllers help keep your code simple by focusing on one job per controller. This makes your app easier to understand and maintain.
Single action controllers in Laravel
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ExampleController
{
public function __invoke(Request $request)
{
// Your code here
}
}The __invoke method is special in PHP and lets the controller be called like a function.
You don't need to define multiple methods; just one __invoke handles the action.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ShowProfileController { public function __invoke(Request $request) { return view('profile.show'); } }
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SubmitFormController { public function __invoke(Request $request) { // Process form data return redirect('/thank-you'); } }
This single action controller returns a simple welcome message when its route is accessed.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class WelcomeController { public function __invoke(Request $request) { return response('Welcome to our site!'); } }
Register single action controllers in routes using the controller class name without specifying a method.
Example route: Route::get('/welcome', WelcomeController::class);
Single action controllers improve readability and reduce boilerplate code.
Single action controllers use the __invoke method to handle one action.
They keep your code focused and easier to maintain.
Use them when your controller only needs to do one thing.
Practice
Solution
Step 1: Understand the role of single action controllers
Single action controllers are designed to handle only one action, making code simpler and focused.Step 2: Identify the method used
They use the special __invoke method to handle that single action.Final Answer:
To handle exactly one action using the __invoke method -> Option CQuick Check:
Single action controller = __invoke method [OK]
- Thinking they handle multiple actions
- Confusing with middleware or migrations
- Assuming they require multiple methods
Solution
Step 1: Recall the special method for single action controllers
Single action controllers use the __invoke() method to handle the action.Step 2: Match the syntax
The correct syntax defines a class with a public function __invoke() method.Final Answer:
class MyController { public function __invoke() {} } -> Option AQuick Check:
__invoke method syntax = class MyController { public function __invoke() {} } [OK]
- Using index() or handle() instead of __invoke()
- Forgetting the public visibility
- Confusing method names with other controller types
class WelcomeController {
public function __invoke() {
return 'Hello, Laravel!';
}
}Solution
Step 1: Understand __invoke method behavior
The __invoke method runs when the controller is called as a single action, returning the string 'Hello, Laravel!'.Step 2: Predict the output
Since the method returns a string, the response will be that string displayed.Final Answer:
Hello, Laravel! -> Option AQuick Check:
__invoke returns string = 'Hello, Laravel!' [OK]
- Expecting an error due to missing method name
- Thinking it returns empty or 404
- Confusing with multi-method controllers
class TestController {
public function invoke() {
return 'Test';
}
}Solution
Step 1: Check method name for single action controller
The method must be named __invoke with two underscores, not invoke.Step 2: Verify method visibility and return
Method should be public and return a value, which is correct here.Final Answer:
Missing __invoke method name with double underscores -> Option DQuick Check:
Method name must be __invoke [OK]
- Using invoke instead of __invoke
- Changing method visibility incorrectly
- Assuming class name affects single action controller
Solution
Step 1: Use __invoke method for single action controller
The controller must have a public __invoke() method to be a single action controller.Step 2: Return a proper JSON response in Laravel
Use Laravel's response()->json() helper to return a JSON response correctly.Final Answer:
class JsonResponseController { public function __invoke() { return response()->json(['message' => 'Success']); } } -> Option BQuick Check:
__invoke + response()->json() = class JsonResponseController { public function __invoke() { return response()->json(['message' => 'Success']); } } [OK]
- Using handle() instead of __invoke()
- Returning raw arrays without response helper
- Using json() function which doesn't exist in Laravel
Ready to prove it?
Pass the test and this pattern moves unknown
