Bird
Raised Fist0
Laravelframework~20 mins

Single action controllers in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a single action controller in Laravel?
easy
A. To replace middleware functionality
B. To manage multiple actions in one controller
C. To handle exactly one action using the __invoke method
D. To handle database migrations

Solution

  1. Step 1: Understand the role of single action controllers

    Single action controllers are designed to handle only one action, making code simpler and focused.
  2. Step 2: Identify the method used

    They use the special __invoke method to handle that single action.
  3. Final Answer:

    To handle exactly one action using the __invoke method -> Option C
  4. Quick Check:

    Single action controller = __invoke method [OK]
Hint: Single action controllers always use __invoke method [OK]
Common Mistakes:
  • Thinking they handle multiple actions
  • Confusing with middleware or migrations
  • Assuming they require multiple methods
2. Which of the following is the correct syntax to define a single action controller in Laravel?
easy
A. class MyController { public function __invoke() {} }
B. class MyController { public function handle() {} }
C. class MyController { public function index() {} }
D. class MyController { public function action() {} }

Solution

  1. Step 1: Recall the special method for single action controllers

    Single action controllers use the __invoke() method to handle the action.
  2. Step 2: Match the syntax

    The correct syntax defines a class with a public function __invoke() method.
  3. Final Answer:

    class MyController { public function __invoke() {} } -> Option A
  4. Quick Check:

    __invoke method syntax = class MyController { public function __invoke() {} } [OK]
Hint: Look for __invoke method in the class [OK]
Common Mistakes:
  • Using index() or handle() instead of __invoke()
  • Forgetting the public visibility
  • Confusing method names with other controller types
3. Given this single action controller code, what will be the output when accessed?
class WelcomeController {
    public function __invoke() {
        return 'Hello, Laravel!';
    }
}
medium
A. Hello, Laravel!
B. Error: Method not found
C. Empty response
D. 404 Not Found

Solution

  1. Step 1: Understand __invoke method behavior

    The __invoke method runs when the controller is called as a single action, returning the string 'Hello, Laravel!'.
  2. Step 2: Predict the output

    Since the method returns a string, the response will be that string displayed.
  3. Final Answer:

    Hello, Laravel! -> Option A
  4. Quick Check:

    __invoke returns string = 'Hello, Laravel!' [OK]
Hint: __invoke returns output directly as response [OK]
Common Mistakes:
  • Expecting an error due to missing method name
  • Thinking it returns empty or 404
  • Confusing with multi-method controllers
4. Identify the error in this single action controller code:
class TestController {
    public function invoke() {
        return 'Test';
    }
}
medium
A. Return statement is invalid
B. Method should be private, not public
C. Class name must be TestActionController
D. Missing __invoke method name with double underscores

Solution

  1. Step 1: Check method name for single action controller

    The method must be named __invoke with two underscores, not invoke.
  2. Step 2: Verify method visibility and return

    Method should be public and return a value, which is correct here.
  3. Final Answer:

    Missing __invoke method name with double underscores -> Option D
  4. Quick Check:

    Method name must be __invoke [OK]
Hint: Method must be named __invoke with double underscores [OK]
Common Mistakes:
  • Using invoke instead of __invoke
  • Changing method visibility incorrectly
  • Assuming class name affects single action controller
5. You want to create a single action controller that returns a JSON response with a message 'Success'. Which code snippet correctly implements this?
hard
A. class JsonResponseController { public function __invoke() { return json_encode(['message' => 'Success']); } }
B. class JsonResponseController { public function __invoke() { return response()->json(['message' => 'Success']); } }
C. class JsonResponseController { private function __invoke() { return ['message' => 'Success']; } }
D. class JsonResponseController { public function handle() { return json(['message' => 'Success']); } }

Solution

  1. Step 1: Use __invoke method for single action controller

    The controller must have a public __invoke() method to be a single action controller.
  2. Step 2: Return a proper JSON response in Laravel

    Use Laravel's response()->json() helper to return a JSON response correctly.
  3. Final Answer:

    class JsonResponseController { public function __invoke() { return response()->json(['message' => 'Success']); } } -> Option B
  4. Quick Check:

    __invoke + response()->json() = class JsonResponseController { public function __invoke() { return response()->json(['message' => 'Success']); } } [OK]
Hint: Use response()->json() inside __invoke method [OK]
Common Mistakes:
  • Using handle() instead of __invoke()
  • Returning raw arrays without response helper
  • Using json() function which doesn't exist in Laravel
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard