Should I use the $response provided by a route?


#1

I just want to make sure: Is there any reason why I should better use the Response object that is provided by a route, or can I just return another one?

$app->get('/datas/{id}', function (Request $request, Response $response) {
    // do stuff here (i.e. guzzle stuff)
    return $response;
}

I get a Response object from guzzle and thought it would be the easiest to just return this one, rather than fixing up the provided one with all the stuff I think it might need(headers, body…) from the one I got from guzzle.

I made my first route this way and it works, but before I do all my other routes, I want to be certain this is a solid way to go.


#2

This is a fine way to go if it makes sense for your application… and it sounds like it does. This is the nice thing about PSR-7… just swap in a response from somewhere else if it has more of what you need.


#3

Wow, that was quick.

I don’t know all the details about those objects yet and wasn’t sure if I would lose data or send wrong data if I would do is this way (especially because the guzzle response comes from another server).

So thanks for the answer.


#4

If you create your own response object then you’ll be undoing any middleware that may have run prior to hitting your app code. Only middleware configured to run after will have an effect. That may be desirable for you (you may not use middleware at all)