0
0
Laravelframework~10 mins

Rate limiting 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 apply a rate limit of 60 requests per minute to a route.

Laravel
Route::middleware('throttle:[1]')->group(function () {
    Route::get('/api/data', function () {
        return response()->json(['message' => 'Success']);
    });
});
Drag options to blanks, or click blank then click option'
A60,1
B60
C1,60
Dthrottle
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one number instead of two separated by a comma.
Swapping the order of numbers (minutes first instead of requests).
2fill in blank
medium

Complete the code to create a custom rate limiter named 'uploads' allowing 10 requests per minute.

Laravel
use Illuminate\Support\Facades\RateLimiter;

RateLimiter::for('uploads', function ([1] $request) {
    return Limit::perMinute(10);
});
Drag options to blanks, or click blank then click option'
ARequest
BRequestHandler
CHttpRequest
DRequestData
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like RequestHandler or HttpRequest.
Not importing the Request class.
3fill in blank
hard

Fix the error in the code to limit requests by user ID instead of IP address.

Laravel
RateLimiter::for('api', function (Request $request) {
    return Limit::perMinute(100)->by($request->[1]());
});
Drag options to blanks, or click blank then click option'
Auser_id
Buser()->id
Cip
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using $request->ip() limits by IP, not user.
Using 'user' or 'user_id' as method names causes errors.
4fill in blank
hard

Fill both blanks to create a rate limiter that limits 5 requests per minute per user email.

Laravel
RateLimiter::for('email_limit', function (Request $request) {
    return Limit::perMinute([1])->by($request->user()->[2]);
});
Drag options to blanks, or click blank then click option'
A5
Bemail
Cid
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using 10 instead of 5 for the limit.
Using 'id' instead of 'email' for the user property.
5fill in blank
hard

Fill all three blanks to define a rate limiter that allows 20 requests per minute per user IP or user ID fallback.

Laravel
RateLimiter::for('complex_limit', function (Request $request) {
    return Limit::perMinute([1])->by($request->ip() ?: $request->user()->[2] ?? $request->[3]());
});
Drag options to blanks, or click blank then click option'
A20
Bid
Cip
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'email' instead of 'id' for user property.
Using wrong method names for IP.