Slim: $response->withJson() returns error: Call to undefined method Slim\Psr7\Response::withJson()

Created on 9 Dec 2020  路  5Comments  路  Source: slimphp/Slim

I have this code:

$app->get('/', function (Request $request, Response $response, $args) use($database){
    $response->withJson([]);
    return $response->withHeader('Content-Type', 'application/json');
});

But it always returns this error:

#0 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(43): {closure}(Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array)
#1 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/Routing/Route.php(384): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array)
#2 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\Routing\Route->handle(Object(Slim\Psr7\Request))
#3 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#4 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/Routing/Route.php(341): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#5 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/Routing/RouteRunner.php(84): Slim\Routing\Route->run(Object(Slim\Psr7\Request))
#6 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(59): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request))
#7 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner))
#8 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(107): class@anonymous->handle(Object(Slim\Psr7\Request))
#9 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(class@anonymous))
#10 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(107): class@anonymous->handle(Object(Slim\Psr7\Request))
#11 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(class@anonymous))
#12 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): class@anonymous->handle(Object(Slim\Psr7\Request))
#13 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#14 /Users/user/Documents/Programming/php/testing/test1/vendor/slim/slim/Slim/App.php(199): Slim\App->handle(Object(Slim\Psr7\Request))
#15 /Users/user/Documents/Programming/php/testing/test1/public/index.php(48): Slim\App->run()
#16 {main}

I'm just following whats on the docs, but it doesnt seem to be working.

Most helpful comment

Here is the solution:

  1. Add this in your composer.json -> slim/http
  2. Add Slim\Http\Response as Response instead Psr\Http\Message\ResponseInterface as Response

And you will be able to use $response->withJson()

All 5 comments

You need to install Slim-Http decorators as per the docs. Paste your index.php so we can help you further.

Here is the solution:

  1. Add this in your composer.json -> slim/http
  2. Add Slim\Http\Response as Response instead Psr\Http\Message\ResponseInterface as Response

And you will be able to use $response->withJson()

@airtonjr This is for Slim 3, right? There isn鈥檛 much information about these helper methods being removed in Slim 4, but apparently they are. What is the usual way to go about this? Define your own PSR7 Request/Response extensions (with methods such as isXhr, write, withRedirect? Or just stop using them since it makes your app dependent on your own implementation and prevent you from switching to another one?
I actually found out about this before even trying to migrate to Slim 4, just because I set up Intelephense, that told me I was using these methods while they weren鈥檛 part of the PSR7 interface. It got me wondering what is the proper way to do this.

This is for Slim 4. You could use the Slim-Http decorators (as per the docs) or you could also use Responder classes (e.g. a custom JsonResponder, HtmlResponder, ImageResponder, PdfResponder etc..) for this purpose.

Oh ok, interesting! Since the mentions of write and withJson among others had disappeared from the docs, and from slimphp/Slim codebase, and I couldn鈥檛 find them either in slimphp/Slim-Psr7, I assumed they were gone. Thanks for the information!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

codeguy picture codeguy  路  4Comments

jwyuen picture jwyuen  路  5Comments

jaklimoff picture jaklimoff  路  4Comments

codeguy picture codeguy  路  3Comments

geggleto picture geggleto  路  4Comments