How is it possible to output Phalcon\Flash\Direct messages?
In bootstrap:
$di->set('flash', function() {
return new \Phalcon\Flash\Direct();
});
In controller:
$this->flash->error('Some shit happened');
In view
<p><?php $this->flash->output() ?></p>
Throws PHP Fatal error: Call to undefined method Phalcon\Flash\Direct::output()
How it is possible to output Phalcon\Flash\Direct messages in my view?
I'm using ajax, so i do not make any redirects, so i don't need to use session to store messages. Currently i wrote following class to handle my needs, not sure if such functionality is available with Flash Direct component, please poke me if i am reinventing the wheel.
Usage DI definition
$di->set('flash', function() {
return new Flash();
});
Controller action context
$this->flash->error('some error happened');
$this->flash->notice('some thing occured while execution operation, but it is not so bad as error');
$this->flash->warning('man, perhaps you are reinventing the wheel? :D');
View context
<?php echo $this->flash; ?> <!-- will output all necessary html with classes -->
Class definition
class Flash
{
/**
* Messages collection
* @var array
*/
protected $messages;
/**
* Css classes collection
* @var array
*/
protected $cssClasses = [
'error' => 'flash-message error',
'notice' => 'flash-message notice',
'success' => 'flash-message success',
'warning' => 'flash-message warning',
];
/**
* Constructor
* @param array $cssClasses
* @return void
*/
public function __construct(array $cssClasses = null)
{
if (!is_null($cssClasses)) {
if (count($cssClasses)) {
foreach($cssClasses as $type => $class) {
$this->cssClasses[$type] = $class;
}
}
}
}
/**
* Returns current messages
* @return array
*/
public function getMessages()
{
return $this->messages;
}
/**
* Adds message to stack
* @param string $type
* @param string $text
* @return Flash
*/
public function message($type, $text)
{
$this->messages[] = [
'type' => $type,
'text' => $text,
];
return $this;
}
/**
* Adds error message to stack
* @param string $text
* @return Flash
*/
public function error($text)
{
$this->messages[] = [
'type' => 'error',
'text' => $text,
];
return $this;
}
/**
* Adds notice message to stack
* @param string $text
* @return Flash
*/
public function notice($text)
{
$this->messages[] = [
'type' => 'notice',
'text' => $text,
];
return $this;
}
/**
* Adds success message to stack
* @param string $text
* @return Flash
*/
public function success($text)
{
$this->messages[] = [
'type' => 'success',
'text' => $text,
];
return $this;
}
/**
* Adds warning message to stack
* @param string $text
* @return Flash
*/
public function warning($text)
{
$this->messages[] = [
'type' => 'warning',
'text' => $text,
];
return $this;
}
/**
* Outputs messages from stack
* @return string
*/
public function output()
{
$output = '';
if (count($this->messages)) {
foreach($this->messages as $message) {
// Configure css class
$cssClass = 'flash-message';
if (!empty($this->cssClasses[$message['type']])) {
$cssClass = $this->cssClasses[$message['type']];
}
// Append output
$output.= '<div class="' . $cssClass . '">'
. $message['text']
. '</div>';
}
}
return $output;
}
/**
* Outputs all messages
* @return string
*/
public function __toString()
{
return $this->output();
}
}
In addition to Phalcon\Flash\Session there is Phalcon\Flash\Direct, that has a similar to your implementation, this is registered by default in Phalcon\DI\FactoryDefault with the name of "flash":
$this->flash->success("Created successfully");
in your view you need to show the output produced within the controller:
<?php echo $this->getContent() ?>
Wooow! never knew about such Phalcon functionality. And i was thinking that all things i output in acton controller are just never used. Thank you very much!
It's cool but how I can do it when i using other view engine? For example, I use Twig. How do I get a flash message in a template?
For everyone who do want to output Phalcon\Flash\Direct in a certain container use this solution:
Your services.php / dependency injector file looks like:
//Set up the flash session service as flash
$di->set('flash', function() {
return new Phalcon\Flash\Session();
});
your index.volt/.phtml looks like:
//output flash messages in volt:
{{ flash.output() }}
//output flash messages in php:
<?php echo $this->flash->output(); ?>
For the above example to work - it might be obvious - but you also need to have the SessionAdapter initialized in your app (usually index.php or services.php):
use Phalcon\Session\Adapter\Files as SessionAdapter;
$di->set('session', function() {
$session = new SessionAdapter();
$session->start();
return $session;
});
@noonecaresreally: Thanks @phalcon : #DocumentationUpdate