File: /var/www/vhosts/creativefellows.nl/fvr.creativefellows.nl/src/Middleware/NotFoundMiddleware.php
<?php
namespace App\Middleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Exception\HttpNotFoundException;
use App\Handler\DefaultErrorHandler;
use App\Domain\Navigation\Service\NavigationReader;
use App\Domain\Page\Service\PageReader;
/**
* Not Found middleware.
*/
final class NotFoundMiddleware implements MiddlewareInterface
{
public function __construct(
ResponseFactoryInterface $responseFactory,
DefaultErrorHandler $errorHandler,
NavigationReader $navigationReader,
PageReader $pageReader
){
$this->responseFactory = $responseFactory;
$this->errorHandler = $errorHandler;
$this->navigationReader = $navigationReader;
$this->pageReader = $pageReader;
}
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
try{
return $handler->handle($request);
}
catch (HttpNotFoundException $httpException) {
$response = $this->responseFactory->createResponse();
$response = $response->withStatus(404);
// empty page
$page = $this->pageReader->getPageData("");
// get menu elements
$navigation = $this->navigationReader->getNavigationData("");
// return error page
return $this->errorHandler->respond($response,$page,$navigation);
}
}
}