Challenge - 5 Problems
Single Action Controller Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this single action controller?
Consider this Laravel single action controller. What will be the HTTP response content when this controller is called?
Laravel
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class WelcomeController { public function __invoke(Request $request) { return response('Hello, Laravel!'); } }
Attempts:
2 left
💡 Hint
Single action controllers use the __invoke method to handle requests.
✗ Incorrect
The __invoke method returns a plain text response with the string 'Hello, Laravel!'. This is the content the client receives.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a single action controller in Laravel?
Select the code snippet that correctly defines a single action controller class in Laravel.
Attempts:
2 left
💡 Hint
Single action controllers use the magic __invoke method.
✗ Incorrect
Only the __invoke method allows the controller to be called as a single action.
❓ state_output
advanced2:00remaining
What is the output after calling this single action controller with a request?
Given this controller, what will be the output when accessed via a web route?
Laravel
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class CounterController { private int $count = 0; public function __invoke(Request $request) { $this->count++; return "Count is: {$this->count}"; } }
Attempts:
2 left
💡 Hint
Each request creates a new controller instance.
✗ Incorrect
The count starts at 0 and increments once per request. Since each request is new, count is always 1.
🔧 Debug
advanced2:00remaining
Why does this single action controller cause a routing error?
This controller is registered in routes/web.php as Route::get('/test', TestController::class); but accessing '/test' gives a 500 error. What is the problem?
Laravel
<?php namespace App\Http\Controllers; class TestController { public function handle() { return 'Test'; } }
Attempts:
2 left
💡 Hint
Single action controllers must have __invoke method.
✗ Incorrect
When routing to a controller class without specifying a method, Laravel calls __invoke. Missing it causes error.
🧠 Conceptual
expert2:00remaining
What is a key advantage of using single action controllers in Laravel?
Choose the best explanation for why developers use single action controllers.
Attempts:
2 left
💡 Hint
Think about controller responsibilities and code clarity.
✗ Incorrect
Single action controllers have one job, making code easier to read, maintain, and test.