Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Slim, issue when returning a response

Tags:

php

slim

I'm trying to return some content using a Response object. These are the interfaces implemented:

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

Then, in a GET function, i create my own JSON, and trying to reurn it as a response:

$app->get('/getpersons', function(Request $request, Response $response, $args){
    $person= new stdClass();

    $person->id = 1;
    $person->name='Name example';
    $person->address = 'Street example';


    return $response->getBody()->write(json_encode($person));
});

But I'm getting the next error:

Return value of Slim\Handlers\Strategies\RequestResponse::__invoke() must
implement interface Psr\Http\Message\ResponseInterface

I don't have any idea what is happening. Any help?

like image 679
zasaz Avatar asked Oct 17 '25 02:10

zasaz


1 Answers

Solved! Must be written like this:

$response->getBody()->write(json_encode($person));
return $response;
like image 68
zasaz Avatar answered Oct 18 '25 16:10

zasaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!