0
0
Laravelframework~10 mins

Single action controllers in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a single action controller class in Laravel.

Laravel
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class WelcomeController extends Controller
{
    public function [1](Request $request)
    {
        return view('welcome');
    }
}
Drag options to blanks, or click blank then click option'
A__invoke
Bhandle
Cindex
Dshow
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'handle' or 'index' instead of '__invoke' for single action controllers.
2fill in blank
medium

Complete the route definition to use a single action controller in Laravel.

Laravel
use App\Http\Controllers\WelcomeController;

Route::get('/', [1]::class);
Drag options to blanks, or click blank then click option'
AWelcomeController
BWelcomeController@index
CWelcomeController@handle
DWelcomeController@__invoke
Attempts:
3 left
💡 Hint
Common Mistakes
Adding '@__invoke' or other method names in the route for single action controllers.
3fill in blank
hard

Fix the error in the single action controller method signature.

Laravel
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ContactController extends Controller
{
    public function [1](Request $request, $id)
    {
        return view('contact', ['id' => $id]);
    }
}
Drag options to blanks, or click blank then click option'
Ahandle
Bindex
C__invoke
Dshow
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names other than '__invoke' in single action controllers.
4fill in blank
hard

Fill both blanks to create a route that uses a single action controller with middleware.

Laravel
use App\Http\Controllers\DashboardController;

Route::get('/dashboard', [1]::class)->[2]('auth');
Drag options to blanks, or click blank then click option'
ADashboardController
Bmiddleware
Cauth
Dhandle
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names like 'handle' instead of 'middleware' for applying middleware.
5fill in blank
hard

Fill all three blanks to define a single action controller class with a constructor and the invoke method.

Laravel
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProfileController extends Controller
{
    private $service;

    public function [1](UserService $service)
    {
        $this->service = [2];
    }

    public function [3](Request $request)
    {
        return view('profile', ['data' => $this->service->getProfile()]);
    }
}
Drag options to blanks, or click blank then click option'
A__construct
B$service
C__invoke
Dhandle
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names like 'handle' instead of '__invoke' for the single action method.
Not assigning the constructor parameter correctly.