I am still working on migration of my project to Slim 4 and have issue with migration Whoops to Slim4. Old implementation of Whoops was deleted, and I am using this solution temporary…
and I am trying to setup new Whoops for Slim4 instead of default error handler as it is in this tutorial in my app style, like this
use Middlewares\Whoops;
use Slim\Middleware\ErrorMiddleware;
app()->add(Whoops::class);
app()->add(ErrorMiddleware::class);
Got error in php error logs
[03-Sep-2021 15:49:57 UTC] PHP Fatal error: Uncaught DI\Definition\Exception\InvalidDefinition: Entry "Slim\Middleware\ErrorMiddleware" cannot be resolved: Entry "Psr\Http\Message\ResponseFactoryInterface" cannot be resolved: the class is not instantiable
Full definition:
Object (
class = #NOT INSTANTIABLE# Psr\Http\Message\ResponseFactoryInterface
lazy = false
)
Full definition:
Object (
class = Slim\Middleware\ErrorMiddleware
lazy = false
__construct(
$callableResolver = get(Slim\Interfaces\CallableResolverInterface)
$responseFactory = get(Psr\Http\Message\ResponseFactoryInterface)
$displayErrorDetails = #UNDEFINED#
$logErrors = #UNDEFINED#
$logErrorDetails = #UNDEFINED#
$logger = (default value) NULL
)
) in /Applications/MAMP/htdocs/flextype/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php:19
Stack trace:
#0 /Applications/MAMP/htdocs/flextype/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(156): DI\Definition\Exception\InvalidDefinition::create( in /Applications/MAMP/htdocs/flextype/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php on line 19
or it is me doing something wrong, or this article is missing something
This tutorial assumes that you configure all dependencies directly within the DI container (PHP-DI). This makes it possible to add and load the middleware and other dependencies via “Autowiring” and only when it’s really needed (on demand). It also makes testing easier etc…
This means that the DI container is not able to resolve the ResponseFactoryInterface, since interfaces can only be autowired if there is a DI container definition for it. To solve this error, you need to add a definition like this:
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Slim\App;
// HTTP factories
ResponseFactoryInterface::class => function (ContainerInterface $container) {
return $container->get(App::class)->getResponseFactory();
},
Please note that this example fetches the ResponseFactory from the Slim App instance. For this reason, the App instance should also be created within the DI container:
use Slim\App;
use Slim\Factory\AppFactory;
// ...
App::class => function (ContainerInterface $container) {
AppFactory::setContainer($container);
return AppFactory::create();
},