NotAllowed exception catch
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
$app->add($app->getContainer()->get(Incoviba\Middleware\NotFound::class));
|
$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));
|
$app->add($app->getContainer()->get(Incoviba\Middleware\Errors::class));
|
||||||
|
22
app/src/Middleware/NotAllowed.php
Normal file
22
app/src/Middleware/NotAllowed.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user