Created
July 24, 2025 19:54
-
-
Save Suven/f2881b19ba17ba99711a05049c3e8671 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Middleware; | |
| use Closure; | |
| use Illuminate\Http\Request; | |
| use Illuminate\Pipeline\Pipeline; | |
| use Symfony\Component\HttpFoundation\Response; | |
| class ConsiderStatic | |
| { | |
| private function handleStateful(Request $request, Closure $next): Response | |
| { | |
| $middlewares = [ | |
| \Illuminate\Session\Middleware\StartSession::class, | |
| \Illuminate\View\Middleware\ShareErrorsFromSession::class, | |
| \Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class, | |
| ]; | |
| return app(Pipeline::class) | |
| ->send($request) | |
| ->through($middlewares) | |
| ->then($next); | |
| } | |
| /** | |
| * Handle an incoming request. | |
| * | |
| * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next | |
| */ | |
| public function handle(Request $request, Closure $next): Response | |
| { | |
| $sessionName = config('session.cookie'); | |
| // Already has a session cookie and thus is stateful | |
| if ($request->hasCookie($sessionName)) { | |
| return $this->handleStateful($request, $next); | |
| } | |
| // Is within the admin area | |
| if ($request->is('cp/*')) { | |
| return $this->handleStateful($request, $next); | |
| } | |
| // Custom stateful routes | |
| if ($request->is('*login*') || $request->is('*debug*')) { | |
| return $this->handleStateful($request, $next); | |
| } | |
| // Stateless | |
| $response = $next($request); | |
| $response->header('Cache-Control', 'public, max-age=3600'); | |
| $response->setEtag(md5($response->getContent())); | |
| return $response; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| return Application::configure(basePath: dirname(__DIR__)) | |
| ->withRouting( | |
| web: __DIR__.'/../routes/web.php', | |
| commands: __DIR__.'/../routes/console.php', | |
| health: '/up', | |
| ) | |
| ->withMiddleware(function (Middleware $middleware) { | |
| $middleware->web(remove: [ | |
| StartSession::class, | |
| ShareErrorsFromSession::class, | |
| ValidateCsrfToken::class, | |
| ]); | |
| $middleware->append(ConsiderStatic::class); | |
| }) | |
| ->withExceptions(function (Exceptions $exceptions) { | |
| // | |
| })->create(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment