0
0
Laravelframework~10 mins

Single action controllers in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Single action controllers
Route receives request
Invoke Single Action Controller
__invoke() method runs
Process request logic
Return response
Send response to browser
The route calls a controller with one __invoke method that handles the request and returns a response.
Execution Sample
Laravel
class ShowProfileController extends Controller {
  public function __invoke($id) {
    return "User profile: $id";
  }
}

Route::get('/profile/{id}', ShowProfileController::class);
This code defines a single action controller that returns a user profile string for a given id.
Execution Table
StepActionInputMethod CalledOutput/Response
1Route matches GET /profile/5/profile/5ShowProfileController::__invokeCalled with id=5
2Controller __invoke runsid=5__invokeReturns 'User profile: 5'
3Response sent to browserUser profile: 5N/ABrowser displays 'User profile: 5'
4EndN/AN/ARequest complete
💡 Request completes after __invoke returns response and it is sent to browser
Variable Tracker
VariableStartAfter Step 1After Step 2Final
idN/A555
responseN/AN/A'User profile: 5''User profile: 5'
Key Moments - 2 Insights
Why does the controller only have one method named __invoke?
Because single action controllers use the __invoke method to handle the request, so no other methods are needed. See execution_table step 2 where __invoke runs.
How does Laravel know to call __invoke automatically?
When the controller class is passed to the route without specifying a method, Laravel calls __invoke by default. This is shown in execution_table step 1 where the route calls ShowProfileController::__invoke.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'id' when __invoke runs?
A5
Bnull
C0
Dundefined
💡 Hint
Check execution_table row 2 under Input column shows id=5
At which step does the response get sent to the browser?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 3 where response is sent to browser
If the route was changed to call a controller with multiple methods, what would change in the execution flow?
ARoute would still call __invoke automatically
BRoute would specify which method to call instead of __invoke
CController would not run any method
DResponse would be sent before method runs
💡 Hint
Refer to concept_flow where route calls __invoke only if no method specified
Concept Snapshot
Single action controllers have one __invoke method.
Routes call the controller class directly.
Laravel runs __invoke automatically.
Simplifies controllers for single tasks.
Response returned from __invoke is sent to browser.
Full Transcript
Single action controllers in Laravel use a special method called __invoke to handle requests. When a route points to a controller class without specifying a method, Laravel calls the __invoke method automatically. This method receives any route parameters and returns a response. The flow starts when the route matches the request URL, then Laravel creates the controller and runs __invoke with parameters. The method processes the request and returns a response string or view. Finally, Laravel sends this response back to the browser. This pattern simplifies controllers that only need to do one thing, like showing a user profile. The example shows a controller returning a string with the user id. The execution table traces each step from route matching to response delivery. Variables like id and response are tracked to show their values during execution. Key points include understanding why __invoke is used and how Laravel calls it automatically. The visual quiz tests understanding of variable values and flow steps. The snapshot summarizes the concept in a few lines for quick review.