Laravel: Generate a Dynamic Sitemap.xml
Generate a sitemap.xml from your database in Laravel — always up to date, no file to maintain by hand.
Protect your Laravel API from abuse with the built-in throttle middleware — limit requests per minute per user or IP.
16 lines · Updated Nov 26, 2026
// routes/api.php
use Illuminate\Support\Facades\Route;
// 60 requests per minute, per authenticated user
Route::middleware(['auth:sanctum', 'throttle:60,1'])->group(function () {
Route::get('/posts', [PostController::class, 'index']);
});
// Stricter limit for login: 5 attempts per minute, per IP
Route::post('/login', [AuthController::class, 'login'])
->middleware('throttle:5,1');
// Named limiter in bootstrap/app.php for a custom rule
// ->withMiddleware(function (Middleware $middleware) {
// $middleware->throttleApi(); // 60/min for all API routes
// });
Any public API will eventually be hit by a buggy client in a loop, a scraper, or someone guessing passwords. Without limits, one bad actor can slow down the app for everybody — or run up your server bill. Laravel's built-in throttle middleware limits how many requests a user or IP address can make per minute, and returns a clean 429 Too Many Requests response when they go over.
Retry-After header telling the client when to try again.Does rate limiting stop DDoS attacks? It helps against abuse from individual clients, but large attacks should be stopped earlier, at a CDN or firewall.
Related: Sanctum token abilities · OWASP Top 10 explained
Paste the route code into routes/api.php. Change 60,1 to your limit and window in minutes. For a named limiter, register it in bootstrap/app.php with RateLimiter::for().
Share this snippet
Generate a sitemap.xml from your database in Laravel — always up to date, no file to maintain by hand.
Send WhatsApp messages from Laravel through a provider API — order updates, OTPs and alerts in a few lines.
Add one search scope to any Eloquent model and search across several columns with a single method call.