Return inside a controller method

I am quite confused with how action methods in controllers work on Phalcon 2 when you specify a return. I am building an API and I'm finding it weird that adding return false after $this->response->setJsonContent(...) in a method, the JSON content is printed but if I use return or return true it is not.

Why does it do that?

public function indexAction()
    $this->response->setJsonContent(['test' => 1]);
    return false;

Without the return false, it prints nothing. With the return false and send() it prints the JSON twice.

Is there a way to print the JSON content automatically without send() but not print it with return false?


It is because return false disable view rendering. You can also do $this->view->disable()

edited Mar '17

Just do

return $this->response->setJsonContent(['test' => 1]);
edited Mar '17

Depending of context, but for Micro app if your handler or middleware returns (bool) false, it will stop middleware / i.e. your response will not be returned.

General rule of thumb is to always return either string from handlers/controllers, or for special cases to return object method result as @Jurigag mentioned. Return false only if you want to stop further execution, but be aware that in some cases even false will not stop further execution.