Slim: Method 'withJson' not found in Psr\Http\Message\ResponseInterface

Created on 12 Dec 2018  路  6Comments  路  Source: slimphp/Slim

Response interface from PSR does not include withJson() method, which is run on it in closure.

Most helpful comment

@archi-tektur

鈥hy don't you create SlimResponseInterface that will extends PSR7 implementation?

This would be against the idea of PSR and interoperability.

Suggestion: create a separate JsonResponse class which implements the PSR-7 interface for response.
(Compare with zend-diactoros and the custom responses: https://docs.zendframework.com/zend-diactoros/v2/custom-responses/)

All 6 comments

withJson() is just implemented in the Slim response for convenience and has to be implemented yourself in some way when you want to use a different PSR7 implementation.
Thats the reason for this note on the doc-block:

https://github.com/slimphp/Slim/blob/a5a2da92d24c2c30384ff89d90008327f9040cb8/Slim/Http/Response.php#L341

Okey, but still - it's not in the interface so IDEs and Rich text editors will throw a warning, why don't you create SlimResponseInterface that will extends PSR7 implementation?

@archi-tektur

鈥hy don't you create SlimResponseInterface that will extends PSR7 implementation?

This would be against the idea of PSR and interoperability.

Suggestion: create a separate JsonResponse class which implements the PSR-7 interface for response.
(Compare with zend-diactoros and the custom responses: https://docs.zendframework.com/zend-diactoros/v2/custom-responses/)

Another option is to type hint to a Slim\Http\Response if you know that it will always be one in your application.

Okay, thanks, to be closed then

Closing as per @archi-tektur's request

Was this page helpful?
0 / 5 - 0 ratings

Related issues

codeguy picture codeguy  路  3Comments

codeguy picture codeguy  路  4Comments

aranel616 picture aranel616  路  3Comments

lwiwala picture lwiwala  路  5Comments

basuke picture basuke  路  3Comments