NotAllowed exception catch

This commit is contained in:
Juan Pablo Vial
2025-02-24 21:36:03 -03:00
parent ecec1dd8df
commit d02732b0dd
2 changed files with 23 additions and 0 deletions

View File

@ -1,3 +1,4 @@
<?php
$app->add($app->getContainer()->get(Incoviba\Middleware\NotFound::class));
$app->add($app->getContainer()->get(Incoviba\Middleware\NotAllowed::class));
$app->add($app->getContainer()->get(Incoviba\Middleware\Errors::class));

View File

@ -0,0 +1,22 @@
<?php
namespace Incoviba\Middleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Exception\HttpMethodNotAllowedException;
class NotAllowed
{
public function __construct(protected ResponseFactoryInterface $responseFactory) {}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
return $handler->handle($request);
} catch (HttpMethodNotAllowedException) {
return $this->responseFactory->createResponse(405, 'Method Not Allowed');
}
}
}