Whoops for Slim4

Hello!

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…

#NOT INSTANTIABLE# Psr\Http\Message\ResponseFactoryInterface

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();
},

Full example

If you have already invoked the $app->addErrorMiddleware(...) method somewhere, then you do not need to add the ErrorMiddleware a second time.

1 Like

Thanks for our replay @odan

I have finally solved it in this way:

ended up using https://github.com/zeuxisoo/php-slim-whoops for mine. pretty decent “light” wrapper at least.

then in my middleware page

use Zeuxisoo\Whoops\Slim\WhoopsMiddleware;
...
// so only add the whoops middleware if my environment is in debug mode, else prod error handler.
if ($debug) { 
        $app->add(new WhoopsMiddleware(array(
            'enable' => true,
            'editor' => function($file, $line) { // https://github.com/zeuxisoo/php-slim-whoops/issues/30 
                return "http://localhost:8091?message=%file:%line"; 
            },
        )));
    } else {
        // prod error handler
        $app->add(ErrorMiddleware::class);
    }

guessing if you want to write it out yourself you can probs add your own middleware for it. but the principal would remain the same.