Flash Session does not remove messages after output

Hi there,

we are Using:

  • Phalcon 1.3.2
  • Flash\Session
  • Volt
  • MultiModuleSetup

The following problem occurs:

We are generating errorMessages in a form with this code:

$this->flash->error($message);

The Form extends Phalcon\Forms\Form

Now in our index.volt-Template we have the output like this: {{ flash.output() }}

Flash is instantiated like this:

$di->set('flash', function() {
return new Flash(array(
    'error'   => 'alert alert-error',
    'success' => 'alert alert-success',
    'notice'  => 'alert alert-info'
));
});

The errorMessage is shown and never removed. It always shows up again and more messages are cumulated so in short time the screen is full of messages. Are we missing something or is this a bug?

Thanks for help,

Björn

Can you show us the full code of where you setup your Flash service (including the "use" operator) and where you produce the error?

This is the app.php (shortened to the necessary parts)

<?php
  use Phalcon\Flash\Session as Flash;
  // use Phalcon\Flash\Direct as Flash;

  class Application extend \Phalcon\Mvc\Application 
  {
    /**
     * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
     */
    protected function _registerServices()
    {
        $di     = new \Phalcon\DI\FactoryDefault();
        $loader = new \Phalcon\Loader();

        /**
         * We're a registering a set of directories taken from the configuration file
         */
        $loader->registerDirs(
            array(
                __DIR__ . '/../apps/Core/Library/'
            )
        )->register();

        /**
         * Start the session the first time some component request the session service
         */
        $di->set('session', function () {
            $session = new Phalcon\Session\Adapter\Files();
            if (!$session->isStarted()) {
                // muting error messages because of already started session
                @$session->start();
            }
            return $session;
        });

        /**
         * Flash service with custom CSS classes
         */
        $di->set('flash', function () {
            return new Flash(array(
                'error'   => 'alert alert-error',
                'success' => 'alert alert-success',
                'notice'  => 'alert alert-info'
            ));
        });

        $this->setDI($di);
    }

    public function main()
    {

        Debug::enable();

        $this->getConfig();
        $this->_registerServices();

        //Register the installed modules
        $this->registerModules(array(
            'partner' => array(
                'className' => 'Exactag\Partner\Module',
                'path'      => '../apps/Partner/Module.php'
            ),
            'admin'   => array(
                'className' => 'Exactag\Admin\Module',
                'path'      => '../apps/Admin/Module.php'
            )
        ));

        echo $this->handle()->getContent();
    }
}

The error is produced in a controller - here we have the SessionController.php:

<?php
namespace Exactag\Admin\Controllers;

  use Exactag\Core\Controllers\BaseController;
  use Exactag\Core\Forms\LoginForm;


 /**
 * Controller used handle non-authenticated session actions like login/logout, user signup, and forgotten passwords
 */
 class SessionController extends BaseController
 {

  public function indexAction(){
      echo "session";
  }

  /**
   * Starts a session in the admin backend
   */
  public function loginAction()
  {
      $form = new LoginForm();

      try {

          if (!$this->request->isPost()) {

              if ($this->auth->hasRememberMe()) {
                  return $this->auth->loginWithRememberMe();
              }
          } else {

              $data = $this->request->getPost('login');

              if ($form->isValid($data) == false) {
                  foreach ($form->getMessages() as $message) {
                      $this->flash->error($message);
                  }
              } else {

                  if ($this->auth->check($data)) {
                      return $this->response->redirect('admin/dashboard');
                  } else {
                      return $this->response->redirect('admin/session/login');
                  }

              }
          }
      } catch (AuthException $e) {
          $this->flash->error($e->getMessage());
      }

      $this->view->form = $form;
  }

  /**
   * Closes the session
   */
  public function logoutAction()
  {
      $this->auth->remove();

      return $this->response->redirect('index/index');
  }
}
edited Sep '14

So is the caught AuthException showing several times? If so are you sure that the exception isn't being thrown several times or that redirects are occurring more than once before the message is displayed?

This is the app.php (shortened to the necessary parts)

Yes, the error is thrown once. If I refresh, the error occurs twice, another refresh and we have 3 and so on. So the problem is that showing the errors with {{ flash.output() }} does not remove them from the session. The API Indece shows a paramter for output:

public output ([boolean $remove])

But this seems to change nothing.



12.7k

I have the similar problem when using session flash with redirect, it constantly show the flash message twice after the first time. Have you find the solution?