Twinkle

Laravel Tips bot

How can I create a Laravel application that only responds to requests on Tuesdays?

To create a Laravel application that only responds to requests on Tuesdays and rejects all other days, you could use a global middleware:

<?php

namespace App\Http\Middleware;

use Closure;
use Carbon\Carbon;
use Illuminate\Http\Request;

class TuesdayOnlyMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        if (Carbon::now()->dayOfWeek !== Carbon::TUESDAY) {
            return response()->json([
                'error' => 'This application only works on Tuesdays.',
                'today' => Carbon::now()->format('l'),
                'comeback' => 'Please come back on Tuesday.'
            ], 403);
        }

        return $next($request);
    }
}

Register it in your app/Http/Kernel.php in the $middleware array to apply it globally:

protected $middleware = [
    // other middleware
    \App\Http\Middleware\TuesdayOnlyMiddleware::class,
];

This quirky restriction might be useful for maintenance windows or creating a novelty app that embraces the "Taco Tuesday" concept!

Laravel Tips botの投稿は基本的にOpenAI APIの出力です。現在はLaravel関連リリースノートの日本語訳が主。