0
0
Laravelframework~10 mins

Registering middleware 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 register a middleware in the HTTP kernel.

Laravel
protected $middleware = [
    \App\Http\Middleware\CheckForMaintenanceMode::class,
    [1]
];
Drag options to blanks, or click blank then click option'
A\App\Http\Middleware\TrimStrings::class
B\App\Http\Middleware\RedirectIfAuthenticated::class
C\App\Http\Middleware\Authenticate::class
D\App\Http\Middleware\VerifyCsrfToken::class
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing middleware that is only for route groups or guards.
Confusing middleware classes that are registered in $routeMiddleware.
2fill in blank
medium

Complete the code to assign middleware to a route group in the kernel.

Laravel
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        [1],
        \App\Http\Middleware\ShareErrorsFromSession::class,
    ],
];
Drag options to blanks, or click blank then click option'
A\App\Http\Middleware\RedirectIfAuthenticated::class
B\App\Http\Middleware\Authenticate::class
C\App\Http\Middleware\VerifyCsrfToken::class
D\App\Http\Middleware\TrimStrings::class
Attempts:
3 left
💡 Hint
Common Mistakes
Using authentication middleware instead of CSRF verification.
Placing middleware in the wrong group.
3fill in blank
hard

Fix the error in registering a route middleware alias.

Laravel
protected $routeMiddleware = [
    'auth' => [1],
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
];
Drag options to blanks, or click blank then click option'
A\Authenticate::class
B\App\Http\Middleware\Authenticate::class
CAuthenticate::class
D\App\Http\Middleware\Authenticate
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the ::class suffix.
Using incomplete namespace paths.
4fill in blank
hard

Fill both blanks to register a custom middleware alias and add it to the 'api' group.

Laravel
protected $routeMiddleware = [
    'checkAge' => [1],
];

protected $middlewareGroups = [
    'api' => [
        'throttle:60,1',
        [2],
    ],
];
Drag options to blanks, or click blank then click option'
A\App\Http\Middleware\CheckAge::class
B\App\Http\Middleware\Authenticate::class
CcheckAge
Dauth
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name instead of the alias in the group.
Forgetting to add the middleware to the group.
5fill in blank
hard

Fill all three blanks to register a middleware alias, add it to a group, and apply it to a route.

Laravel
protected $routeMiddleware = [
    [1] => \App\Http\Middleware\LogAccess::class,
];

protected $middlewareGroups = [
    'web' => [
        [2],
    ],
];

Route::middleware('[3]')->group(function () {
    Route::get('/dashboard', function () {
        return view('dashboard');
    });
});
Drag options to blanks, or click blank then click option'
Alog.access
Dauth
Attempts:
3 left
💡 Hint
Common Mistakes
Using different alias names in different places.
Forgetting to add the middleware to the group or route.