Slim4-Response & Middleware Problem

Hello, I’m migrating Slim2 to Slim 4 and I have a problem with my middleware before routing that load the correct version file of routes. The handle function return an error and I don’t understand why It tell me that it return a Slim\Http\Response instead of a Slim\Psr7\Response. In my uses, i precise that i use Slim\Psr7\Response. Any ideas ?

$routeLoaderMiddleware = function (Request $request, RequestHandler $handler) use ($app, $version):Response{
    $version = getVersion($request);
    include_dir(APP_PATH . '/controllers/v'.$version);
    $setupRoute = "v$version\\setupRoutes";
    $setupRoute($app);
    $response = $handler->handle($request);
    return $response;
};
<h1>Slim Application Error</h1>
	<div>
		<p>The application could not run because of the following error:</p>
		<h2>Details</h2>
		<div><strong>Type:</strong> TypeError</div>
		<div><strong>Code:</strong> 0</div>
		<div><strong>Message:</strong> Closure::v1\{closure}(): Argument #2 ($response) must be of type
			Slim\Psr7\Response, Slim\Http\Response given, called in
			/var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php
			on line 43</div>
		<div><strong>File:</strong> /var/www/html/myproject/vendor/sites/api/app/controllers/v1/routes.php</div>
		<div><strong>Line:</strong> 191</div>
		<h2>Trace</h2>
<pre>#0 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(43): Closure-&gt;v1\{closure}()
#1 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/Routing/Route.php(384): Slim\Handlers\Strategies\RequestResponse-&gt;__invoke()
#2 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\Routing\Route-&gt;handle()
#3 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\MiddlewareDispatcher-&gt;handle()
#4 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/Routing/Route.php(341): Slim\MiddlewareDispatcher-&gt;handle()
#5 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/Routing/RouteRunner.php(84): Slim\Routing\Route-&gt;run()
#6 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/Middleware/BodyParsingMiddleware.php(68): Slim\Routing\RouteRunner-&gt;handle()
#7 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\BodyParsingMiddleware-&gt;process()
#8 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(59): Psr\Http\Server\RequestHandlerInterface@anonymous-&gt;handle()
#9 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware-&gt;process()
#10 /var/www/html/myproject/vendor/sites/api/public/index.php(74): Psr\Http\Server\RequestHandlerInterface@anonymous-&gt;handle()

Have you tried to use the PSR-7 ServerRequestInterface interface as request instead?

use Psr\Http\Message\ServerRequestInterface;
// …

$routeLoaderMiddleware = function (ServerRequestInterface $request, RequestHandler $handler) {…

Yes, I use it already.

use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Routing\RouteContext;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Exception\HttpNotFoundException;
use Slim\Psr7\Response;
use Selective\BasePath\BasePathMiddleware;
use Selective\BasePath\BasePathDetector;


Oh, now I see that you use use Slim\Psr7\Response; but your action handler returns a Slim\Http\Response object. Try to replace use Slim\Psr7\Response; with the generic interface:

use Psr\Http\Message\ResponseInterface as Response;

With this, I obtain a new stacktrace but what i return to write in the response body is the datas json encoded not an int.

 return json_encode($json);
<h1>Slim Application Error</h1>
	<div>
		<p>The application could not run because of the following error:</p>
		<h2>Details</h2>
		<div><strong>Type:</strong> TypeError</div>
		<div><strong>Code:</strong> 0</div>
		<div><strong>Message:</strong> Slim\Handlers\Strategies\RequestResponse::__invoke(): Return value must be of
			type Psr\Http\Message\ResponseInterface, int returned</div>
		<div><strong>File:</strong>
			/var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php
		</div>
		<div><strong>Line:</strong> 43</div>
		<h2>Trace</h2>
		<pre>#0 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/Routing/Route.php(384): Slim\Handlers\Strategies\RequestResponse-&gt;__invoke()
#1 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\Routing\Route-&gt;handle()
#2 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\MiddlewareDispatcher-&gt;handle()
#3 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/Routing/Route.php(341): Slim\MiddlewareDispatcher-&gt;handle()
#4 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/Routing/RouteRunner.php(84): Slim\Routing\Route-&gt;run()
#5 /var/www/html/myproject/vendor/sites/api/public/index.php(83): Slim\Routing\RouteRunner-&gt;handle()
#6 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/MiddlewareDispatcher.php(313): Closure-&gt;{closure}()
#7 /var/www/html/myproject/vendor/sites/api/vendor/akrabat/ip-address-middleware/src/IpAddress.php(132): Psr\Http\Server\RequestHandlerInterface@anonymous-&gt;handle()
#8 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): RKA\Middleware\IpAddress-&gt;process()
#9 /var/www/html/myproject/vendor/sites/api/vendor/selective/basepath/src/BasePathMiddleware.php(52): Psr\Http\Server\RequestHandlerInterface@anonymous-&gt;handle()
#10 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Selective\BasePath\BasePathMiddleware-&gt;process()
#11 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(107): Psr\Http\Server\RequestHandlerInterface@anonymous-&gt;handle()
#12 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware-&gt;process()
#13 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Psr\Http\Server\RequestHandlerInterface@anonymous-&gt;handle()
#14 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher-&gt;handle()
#15 /var/www/html/myproject/vendor/sites/api/vendor/slim/slim/Slim/App.php(199): Slim\App-&gt;handle()
#16 /var/www/html/myproject/vendor/sites/api/public/index.php(119): Slim\App-&gt;run()
#17 {main}</pre>

Why not just use the response object? Note that the write() method returns an int and not the new response object. Usage:

$response->getBody()->write(json_encode($data));

return $response->withHeader('Content-Type', 'application/json');

Ah, yes thanks. The stacktrace was due to my mistake :

$response = $response->getBody()->write(json_encode($data));

It was an error. now it works thanks.

$response->getBody()->write(json_encode($data));

return $response
1 Like