We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

A question about json response

Dear all, I am working a app backend using Phalcon.
I always return a json result to http request, I wonder which way is more appropriate below? Which one is better?

First way

There is no return

public function someAction() {
    //do something
    $this->response->setStatusCode(200, 'OK');
    $this->response->setJsonContent(['some' => 'value']);
    $this->response->send();
}

Second way

There is return send

public function someAction() {
    //do something
    $this->response->setStatusCode(200, 'OK');
    $this->response->setJsonContent(['some' => 'value']);
    return $this->response->send();
}


58.4k

In fist way it send response to the client. if you need to return a response directly from a controller , you can second way



1.5k
edited Nov '14

Hello, Is the second way more faster than the first way?

In fist way it send response to the client. if you need to return a response directly from a controller , you can second way

Our app does neither.

A controller can return a response object. If you return anyting else it becomes a "possible response".

The phalcon application class will get the response object and call send anyway, so you could reduce some typing by simply removing the third line. Also 200 OK should be the default so you should not need to set that.