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

Should I use $this->response or instantiate a new Response?

Hello there,

I recently noticed a that Phalcon\Mvc\Controller contains a field $response, should I guess this question is kind of self answering, but is it good practice to use this $response field or should I instantiate a new Phalcon\Http\Response by myself?

EG:

class FooController extends Controller{
    function testAction(){
        $response = new Phalcon\Http\Response();
        //Do Some Stuff
        $response->setContent($content);
        $response->setStatusCode(200, 'Ok');
        return $response;
    }
}

or

class FooController extends Controller{
    function testAction(){
        //Do Some Stuff
        $this->response->setContent($content);
        $this->response->setStatusCode(200, 'Ok');
        return $this->response;
    }
}


6.2k
Accepted
answer

Use "$this->response" for sure and you don't need to return it in an action method.

Thanks for the advice!