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.

How to print flash sessions or flash directs like a JSON string?

hi guys,

attention to Phalcon Docs

$di->set(
    "flash",
    function () {
        $flash = new FlashDirect(
            [
                "error"   => "alert alert-danger",
                "success" => "alert alert-success",
                "notice"  => "alert alert-info",
                "warning" => "alert alert-warning",
            ]
        );

        return $flash;
    }
);

i want to print alerts and messages like a json string :

[["success","Created Successfully"],["warning","Name may be incorrect"],...]

how can i prevent printing default html template of flashsession<div class=.... ?



35.8k
Accepted
answer
edited Jun '17
class FlashJson extends \Phalcon\Flash\Direct {
    public function outputJson() {
        $json = json_encode($this->_messages);
        if($this->_implicitFlush) {
            echo $json;
        }
        return $json;
    }
}

You want to call this explicitly instead of the default in your template file.

You can also preprocess the messages here to fit your wanted structure.