Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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;
    }
}


3.8k
Accepted
answer

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

Thanks for the advice!