How to utilize Slim-Http Decorators in Slim 4

How do you utilize Slim-Http Decorators in Slim 4? I can’t figure it out despite finding information at the following URLs.

To help make it concrete, I’ve created a GitHub repository with a simple sample Slim 4 application. It currently throws an error. I prefer a pull request that gets this sample working, but if you want to describe the changes, I’ll make them and commit the changes if it’s working and hopefully, this can serve as an example for others trying to get this working.

Thanks,
Chad

In Slim 4 you need to define a DI container definition for the Psr\Http\Message\ResponseFactoryInterface.

Example for instantiating a Decorated Nyholm/Psr7 Response:

<?php

use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Slim\Http\Factory\DecoratedResponseFactory;

return [
    ResponseFactoryInterface::class => function (ContainerInterface $container) {
        $nyholmFactory = $container->get(Psr17Factory::class);

        return new DecoratedResponseFactory($nyholmFactory, $nyholmFactory);
    },
];

Ok, I got this simple app working. Without changing any of the code!

What was the issue?

When I installed Slim-Http with composer with the following, composer installed version 0.4 without error.

composer require slim/http

I assumed that composer would install the latest stable version of the software. so I didn’t even notice the version number. Turns out that version 0.4 has no DecoratedServerRequestFactory or DecoratedResponseFactory. The latest version of Slim-Http is 1.3. When I tried to force that version with the following command I discovered that the newer versions of Slim-Http require the PHP extensions simplexml and fileinfo.

composer require slim/http:1.3

Because these extensions were missing, composer thought that it would be fine to use version 0.4.

Since I’m running this in a docker container on Alpine Linux, the fix is simply to:

apk add php81-simplexml php81-fileinfo

Then, remove and reinstall slim/http with composer.

Relevant Slim 4 Implementation Details

While adding echo statements all over Slim to figure out what’s going on, I found out the follow that may be helpful for others to know.

Slim-Http will decorate Nyholm, but it will also decorate slim/psr7 and probably the others too.

At least the isGet method worked. I haven’t done extensive testing.

You do not need any dependency injection classes or configuration for this to work.

App.run uses Slim\Factory\ServerRequestCreatorFactory to get the right Psr7 Request object. ServerRequestCreatorFactory uses Psr17FactoryProvider to look for the Psr7 implementation that is installed slim/psr7 nyholm/psr7, guzzlehttp, and laminas, then it will automatically wrap the ServerReqeust if (a recent version of) Slim-Http is installed.

The AppFactory class in Slim 4 does the same thing for creating Response objects.

1 Like