We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

How to prevent Controller from sending view

I'm communicating with my controller via AJAX and requesting an action. But depending to the result on the said controller, I want it to return particular values, but by now it return a whole "index" template.

I just want it to return what I say it to return. How can I do that?

Example:

// In some view...
$.post('my/action', send, function(res){
    // 'res' variable should be 'true' or 'false', not the whole View html, etc...
});


33.8k
Accepted
answer
edited Sep '14
$this->view->disable();
$response = new Phalcon\Http\Response(),

// Do things here.
$varWithThingsDone = 'Doing things.';

$response->setJsonContent(array(
    'response' => $varWithThingsDone
));

You might want to know that you can also check if the post is from AJAX:

$this->request->isAjax();

@RompePC perfect, worked like a charm, thanks.