0
0
Laravelframework~20 mins

Single action controllers in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Single Action Controller Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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!');
    }
}
AA 404 Not Found error response
BA redirect response to the home page '/'
CA plain text HTTP response with content 'Hello, Laravel!'
DA JSON response with content {"message": "Hello, Laravel!"}
Attempts:
2 left
💡 Hint
Single action controllers use the __invoke method to handle requests.
📝 Syntax
intermediate
2: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.
Aclass UserController { public function __invoke() { return 'User'; } }
Bclass UserController { public function handle() { return 'User'; } }
Cclass UserController { public function index() { return 'User'; } }
Dclass UserController { public function __call() { return 'User'; } }
Attempts:
2 left
💡 Hint
Single action controllers use the magic __invoke method.
state_output
advanced
2: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}";
    }
}
ACount is: 1
BCount is: 2
CCount is: 0
DError: Cannot access private property
Attempts:
2 left
💡 Hint
Each request creates a new controller instance.
🔧 Debug
advanced
2: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';
    }
}
AThe controller namespace is incorrect.
BThe handle method is private and cannot be accessed.
CThe route should use a closure, not a controller class.
DThe controller lacks an __invoke method required for single action controllers.
Attempts:
2 left
💡 Hint
Single action controllers must have __invoke method.
🧠 Conceptual
expert
2:00remaining
What is a key advantage of using single action controllers in Laravel?
Choose the best explanation for why developers use single action controllers.
AThey enable automatic database migrations when routes are registered.
BThey simplify controllers by focusing on one action, improving readability and testability.
CThey automatically cache responses without extra code.
DThey allow multiple HTTP methods to be handled in one controller method.
Attempts:
2 left
💡 Hint
Think about controller responsibilities and code clarity.