I created the renderer in:
/src/Error/Renderer/HtmlErrorRenderer.php
My /config/middleware.php:
<?php
use Slim\App;
use Slim\Middleware\ErrorMiddleware;
use Selective\BasePath\BasePathMiddleware;
use App\Error\Renderer\HtmlErrorRenderer;
return function (App $app) {
// Parse json, form data and xml
$app->addBodyParsingMiddleware();
// Add the Slim built-in routing middleware
$app->addRoutingMiddleware();
//$app->setBasePath('/');
$app->add(BasePathMiddleware::class); // <--- here
// Catch exceptions and errors
$app->add(ErrorMiddleware::class);
/*
// tried the lines below instead of the line above - no luck
$errorMiddleware = $app->addErrorMiddleware(false, true, true);
$errorHandler = $errorMiddleware->getDefaultErrorHandler();
$errorHandler->registerErrorRenderer('text/html', HtmlErrorRenderer::class);
*/
};
I also tried using this code within the container but that didn’t work.
Can anyone help me with this? I’m thinking the container is a better place for this but I couldn’t wrong entirely.
Thank you very much for your reply. I figured this out myself before I read your post, the main issue I was having was in the renderer:
final class HtmlErrorRenderer extends ErrorRendererInterface
should be:
final class HtmlErrorRenderer
This is what I ended up with in the DI, this works:
// at the top of the script
use App\Error\Renderer\HtmlErrorRenderer;
ErrorMiddleware::class => function (ContainerInterface $container) {
$app = $container->get(App::class);
$settings = $container->get('settings')['error'];
$errorMiddleware = new ErrorMiddleware(
$app->getCallableResolver(),
$app->getResponseFactory(),
(bool)$settings['display_error_details'],
(bool)$settings['log_errors'],
(bool)$settings['log_error_details']
);
$errorHandler = $errorMiddleware->getDefaultErrorHandler();
$errorHandler->registerErrorRenderer('text/html', HtmlErrorRenderer::class);
return $errorMiddleware;
},