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 I can access to flashSession service from my own helpers?

I want to access to flashSession messages from other helper, It's like a flashSession.output() method but I want to change all the html doing some checks, etc...

I want this:

```
{% if flashSession.has() %}
    {% if flashSession.has("error") %}
      {% set bgColorClass = "red lighten-4" %}
      {% set textColorClass = "red-text text-darken-4" %}
      {% set flashIcon = "error" %}
    {% elseif flashSession.has("success") %}
      {% set bgColorClass = "green lighten-4" %}
      {% set textColorClass = "green-text text-darken-4" %}
      {% set flashIcon = "success" %}
    {% elseif flashSession.has("notice") %}
      {% set bgColorClass = "blue lighten-4" %}
      {% set textColorClass = "blue-text text-darken-4" %}
      {% set flashIcon = "notice" %}
    {% elseif flashSession.has("warning") %}
      {% set bgColorClass = "yellow lighten-4" %}
      {% set textColorClass = "yellow-text text-darken-4" %}
      {% set flashIcon = "warning" %}
    {% endif %}
    <div class="row flash">
      <div class="col s12">
        <div class="card-panel {{ bgColorClass }}">
          <span class="{{ textColorClass }}">
            <i class="material-icons">{{ flashIcon }}</i>
            <div class="flash-messages">
              {{ flashSession.output() }}
            </div>
          </span>
        </div>
      </div>
    </div>
{% endif %}
´´´

in this way:

```
{{ custom.showFlashMessages() }}
´´´

Thanks!



35.8k
Accepted
answer
edited Aug '15

I'd go with extending the existing class: https://docs.phalconphp.com/en/latest/api/Phalcon%5Flash%5Session.html (Also, your proposed implementation doesn't handle multiple types of messages, just mentioning)

namespace Whatever;

use Phalcon\Flash\Session as PHFlashSession;

class FlashSession extends PHFlashSession {

   protected static $_myStyle = array(
      'error' => array('red lighten-4', 'red-text text-darken-4', 'error'),
      'warning' => array('yellow lighten-4', 'yellow-text text-darken-4', 'warning'),
      'notice' => array('blue lighten-4', 'blue-text text-darken-4', 'notice'),
      'success' => array('green lighten-4', 'green-text text-darken-4', 'success'),
   );

   public function output($remove = true) {
      if(!$this->has()) {
          return;
      }
      $html = '';
      foreach($this->getMessages(null, $remove) as $type => &$message) {
         $html. = '
<div class="row flash">
<div class="col s12">
    <div class="card-panel '.self::$_myStyle[$type][0].'">
        <span class="'.self::$_myStyle[$type][1].'">
            <i class="material-icons">'.self::$_myStyle[$type][2].'</i>
            <div class="flash-messages">
                '.$message.'
            </div>
        </span>
    </div>
</div>
</div>';
      }
      $html. = '';
      echo $html;
   }

}
// services.php
$di->set('flashSession', function() {
   return new \Whatever\FlashSession;
});
controller/view.volt
{{ flashSession.output() }}


797
edited Aug '15

You can always access DI in static way so in your helper you can do something like Di::getDefault->get('flashSession'), depends how many methods you gonna have in your helper its sometimes better to use Di in static way, but if its gonna be big i thinks extending Phalcon\Flash\Session is better.