From 5bb2778fb7177f193f7cd0eef2bc443822b27e54 Mon Sep 17 00:00:00 2001 From: "Simon L." Date: Tue, 5 May 2026 17:30:07 +0200 Subject: [PATCH] aio-interface: add custom error handler for 405 errors Signed-off-by: Simon L. --- php/public/index.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/php/public/index.php b/php/public/index.php index eb2a7878..7ef6169e 100644 --- a/php/public/index.php +++ b/php/public/index.php @@ -245,6 +245,7 @@ $app->get('/', function (\Psr\Http\Message\RequestInterface $request, Response $ } }); +// Default error handler $errorMiddleware = $app->addErrorMiddleware(false, true, true); // Set a custom Not Found handler, which doesn't pollute the app output with 404 errors. @@ -254,6 +255,17 @@ $errorMiddleware->setErrorHandler( $response = $app->getResponseFactory()->createResponse(); $response->getBody()->write('Not Found'); return $response->withStatus(404); - }); + } +); + +// Set another custom error handler, which doesn't pollute the app output with 405 errors. +$errorMiddleware->setErrorHandler( + \Slim\Exception\HttpMethodNotAllowedException::class, + function (Request $request, Throwable $exception, bool $displayErrorDetails) use ($app) { + $response = $app->getResponseFactory()->createResponse(); + $response->getBody()->write('Method not allowed'); + return $response->withStatus(405); + } +); $app->run();