phalcon flash messages not displaying

Hello everyone. I have a little problem with phalcon's flash messages. 1) I've set up flash in DI like so:

    $di->set('flash', function (){
      $flash = new \Phalcon\Flash\Session([

          //tie in with twitter bootstrap classes
          'error'     => 'alert alert-danger',
          'success'   => 'alert alert-success',
          'notice'    => 'alert alert-info',
          'warning'   => 'alert alert-warning'
      ]);
      return $flash;
    });

Then, I am making an redirect if user doesn't have a permission to access particular area.

    if ($allowed != Acl::ALLOW) {
            $this->flash->error("You do not have permission to access this area.");

            $this->response->redirect('site');

        }

And in my view template I try to ouput them like so:

{{ flash.output() }}

When I debugged my code I noticed that in my Permission class (where I handle ACL and redirect) after setting the message it exists, but when I go to a Site controller it dissapears even though it is set in a session. Whats the problem?

edited Jun '14

Common issue this one. I still have a problem where the CSS dissapears for the flash message. Make sure that you have {{ content() }} in your FINAL rendered view if you are using volt or <?php echo $this->getContent() ?> if you're sticking to the old PHP. There has to be a content() output all the way through any hierarchy; if your are using layouts for instance. In regards to losing styling along the way I had to cheat and put a $this->flash->output(); in the initialize() method of my BaseController.

    <?php

    use Phalcon\Mvc\Controller,
    Phalcon\Tag;

    class BaseController extends Controller {

        /**
        * Main init function
        */
        public function initialize() {
            #code ...
            $this->flash->output();
        }

        #etc etc...
    }

But it is just completely cheating. I have yet to get flash working fully in a within a nice MVC context. :(



16.6k

Didn't work. The problem is that flash magically dissapears after a redirect, therefore there is nothing to output . Is it a bug?

No it's more of a pain in the arse to get going than a bug. Hanve you got your session set before setting the flash in your injector? Here is mine which is working for me.

    /**
    * Start the session
    */

    $di->set('session', function() {
        $session = new \Phalcon\Session\Adapter\Files();
        $session->start();
        return $session;
    }, true);

    /**
    * Register the flash service with custom CSS classes
    */

    $di->set('flash', function() {
        $flash = new \Phalcon\Flash\Session([
            'error' => 'alert alert-danger',
            'success' => 'alert alert-success',
            'notice' => 'alert alert-info',
        ]);

        return $flash;
    });


4.1k
edited Jun '14

See. They advise to disable view rendering.

Your problem is favicon.ico ... probably. Upload it on your server or always return 200 http code in your .htaccess or nginx.conf