I know this is old but it come top in search results so I'm going to take you through what I do. I have a situation where I need just plain old string output to integrate into a javacript call to Materialize.toast (which is a bit like a bootstrap style framework and has a little toast pop up feature, see https://materializecss.com/dialogs.html).
The html in the flash messages was making life difficult so I made my own Messages component:
within my components directory I created Messages.php
<?php
use Phalcon\Mvc\User\Plugin;
/**
* String only messages as an alternative to html formatted flash messages.
*/
class Messages extends Plugin {
public function set($message = null){
if(empty($message)) return false;
$this->session->set("message", $message);
}
public function has() {
return $this->session->has("message");
}
public function output() {
if($this->has()){
$return = $this->session->get("message");
$this->session->remove("message");
return $return;
}
return false;
}
}
Extending Plugin
allows me to use $this->session to handle the storage of the message.
I register this class (look up Phalcon Loader) and then use it in my services.php file:
$di->set('messages', function() {
$messages = new Messages();
return $messages;
}, true);
So I have set
, has
and output
method I can use in my views. For example, say a log in fails we can set the message with $this->messages->set("Sorry, try again");
and in our view (in volt):
{% if messages.has() %}
<script type="text/javascript">
Materialize.toast("{{ messages.output() }}", 4000);
</script>
{% endif %}
Hey presto, I have my toast popup! You can also simply just use {{ messages.output() }}
within your view.