# Slim v4 - Function for Response

**URL:** <https://discourse.slimframework.com/t/slim-v4-function-for-response/3766>\
**Category:** Questions\
**Created:** [December 29, 2019, 6:48pm UTC](https://discourse.slimframework.com/t/slim-v4-function-for-response/3766 "2019-12-29T18:48:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobiastromm](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@tobiastromm](https://discourse.slimframework.com/u/tobiastromm)\
**Post date:** [December 29, 2019, 6:48pm UTC](https://discourse.slimframework.com/t/slim-v4-function-for-response/3766/1 "2019-12-29T18:48:49Z")

</div>

Hi.

I am updating Slim **v2.6.4** to **v4.3.0**.

All is working, except that _I having trouble to create a function to create response_.

**With v2.6.4 I was using the following approach:**

$app-\>post(’/hello’, function () use ($app) {

```
  $nome = $app->request->post('nome');
  $sobrenome = $app->request->post('sobrenome');

  $resposta = array();
  $resposta["nome"] = $nome;
  $resposta["sobrenome"] = $sobrenome;

  RespostaAplicativo(200, $resposta);

```

});

function RespostaAplicativo($status\_code, $response)  
{  
$app = \Slim\Slim::getInstance();  
// Http response code  
$app-\>status($status\_code);  
// setting response content type to json  
$app-\>contentType(‘application/json’);  
echo json\_encode($response);  
}

**This is what I am trying with v4.3.0**

$app-\>post(’/hello’, function (Request $request, Response $response, $args) {

$body = $request-\>getParsedBody();

$nome = $body[‘nome’];  
$sobrenome = $body[‘sobrenome’];

$resposta\_aplicativo = array();  
$resposta\_aplicativo[“nome”] = $nome;  
$resposta\_aplicativo[“sobrenome”] = $sobrenome;

/\*

```
  **WORKING CODE, BUT, NEED TO WORK AS FUNCTION**

  $payload = json_encode($resposta_aplicativo);
  $response->getBody()->write($payload);
  return $response
     ->withHeader('Content-Type', 'application/json')
     ->withStatus(200);

```

\*/

RespostaAplicativo(201, $resposta\_aplicativo, $response);

});

function RespostaAplicativo($status\_code, $dados, Response $response)  
{

$payload = json\_encode($dados);  
$response-\>getBody()-\>write($payload);  
return $response  
-\>withHeader(‘Content-Type’, ‘application/json’)  
-\>withStatus($status\_code);

}

**Type:** TypeError  
**Code:** 0  
**Message:** Return value of Slim\Handlers\Strategies\RequestResponse::\_\_invoke() must implement interface Psr\Http\Message\ResponseInterface, null returned  
**File:** vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php  
**Line:** 42

---

<div class="post-metadata">

**Author:** ![cess11](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@cess11](https://discourse.slimframework.com/u/cess11)\
**Post date:** [December 30, 2019, 9:10am UTC](https://discourse.slimframework.com/t/slim-v4-function-for-response/3766/2 "2019-12-30T09:10:31Z")

</div>

At a glance it seems like the problem is that you don’t return the response returned by RespostaAplicativo(), hence your inline function for /hello returns null instead of the response.

---

<div class="post-metadata">

**Author:** ![tobiastromm](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@tobiastromm](https://discourse.slimframework.com/u/tobiastromm)\
**Post date:** [December 30, 2019, 1:39pm UTC](https://discourse.slimframework.com/t/slim-v4-function-for-response/3766/3 "2019-12-30T13:39:24Z")

</div>

OMG.

So stupid 😅 😂

You are right. Thank u soo much cess11.

This work as expected.

**from**  
RespostaAplicativo(201, $resposta\_aplicativo, $response);  
**to**  
return RespostaAplicativo(201, $resposta\_aplicativo, $response);
