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

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=.... ?



77.7k
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.