We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon 4.0.5 FlashSession Issues

So I'm running into a major headache with using FlashSession in Phalcon 4.0.5. Searched for a couple hours on these forums and on Google, yet I can't seem to find any help on the issue. I'm hoping someone can shed some insight on this.

I'm running a multi-module setup and currently working on the backend module. I've built a full CMS in Phalcon 3.4 and never had issues setting up and using Flash Session messages before, so it's beyond me why it isn't working now.

Here's my backend Module code to set the flashSession service:

        $app->set('flashSession', function() {

            $session = new Manager();
            $files = new Stream(
                [
                    'savePath' => '../core/tmp',
                ]
            );
            $session->setAdapter($files);

            $escaper = new Escaper();
            $flashSession = new FlashSession($escaper, $session);

            return $flashSession;
        });

As a note, I am using "setAdapter" rather than "setHandler", as mentioned in the docs, because using "setHandler" generates an undefined function error.

And here is my simple DB update function where I want to let the user know with a session message that the update was successful after being sent back to the main index action of the "forms" controller:

    public function updateAction() {
        $this->view->disable();

        if($this->request->isPost()) {
            // Get POST fields (sanitizes them automatically)
            $formId = $this->request->getPost('form_id');
            $formName = $this->request->getPost('form_name');

            // Initialize Database Model and set column values for insert
            $form = Forms::findFirst($formId);
            $form->name         = $formName;
            $form->date_updated = time();

            if($form->update() == false) {
                $this->view->disable();
                foreach ($form->getMessages() as $message) {
                    echo '<p>';
                    echo 'Message: '.$message->getMessage().'<br />';
                    echo 'Field: '.$message->getField().'<br />';
                    echo 'Type: '.$message->getType().'<br />';
                    echo '</p>';
                }
            } else {
                $this->flash->success('The form has been created successfully!');
                return $this->response->redirect('forms');
            }           
        }
    }

In my main index.volt layout, I am using {{ flashSession.output() }}, but I get nothing after being redirected.

Regardless of using or removing $this->view->disable();, the page will just redirect without the message.

I have tried to use $this->dispatcher->forward() with the appropriate array and even then it does not display the flash message.

The output only works when removing the redirect, but obviously that's not ideal because I need to send them back to the main index action.

Any help is greatly appreciated.

Change

$this->flash->success('The form has been created successfully!');

To

$this->flashSession->success('The form has been created successfully!');

As you declared service $app->set('flashSession'

edited May '20

Change

$this->flash->success('The form has been created successfully!');

To

$this->flashSession->success('The form has been created successfully!');

As you declared service $app->set('flashSession'

Good catch! I just changed that and unfortunately it looks like we went a step backwards.

Now using

$this->flashSession->success('The form has been created successfully!');

doesn't display anything, even when removing the redirect. This is an odd one...

Inside service declaration add line

$flashSession->setImplicitFlush(true); // echo instead of return
edited May '20

Inside service declaration add line

$flashSession->setImplicitFlush(true); // echo instead of return

Hey Anton, I tried the following.

1.) Placed $flashSession->setImplicitFlush(true); inside the service declaration before returning $flashSession

2.) Using return $flashSession->setImplicitFlush(true); instead of just return $flashSession;

3.) Defining $session and $escaper outside of the DI of the main bootstrap file and then using them with the use operator when defining the shared service (rather than declaring just in the module). This is a rundown of how it is now in index.php:


use Phalcon\Session\Manager;
use Phalcon\Session\Adapter\Stream;
use Phalcon\Escaper;
use Phalcon\Flash\Session as FlashSession;
$app = new FactoryDefault();

$escaper   = new Escaper();
$session   = new Manager();
$files     = new Stream(
    [
        'savePath' => '../core/tmp',
    ]
);
$session->setAdapter($files);

        $app->setShared('flashSession', function() use($session, $escaper) {

            $flashSession = new FlashSession($escaper, $session);

            return $flashSession;
        });

All three of these methods didn't produce anything, so I'm at a loss. Sorry if I'm dense and just not getting something simple here. I really appreciate you help though! :)

edited May '20

One cool thing about it is that performance-wise, it is arguably much faster than other frameworks out there. The reason is that it is not your ordinary PHP framework whose files you just copy onto your server and you are good to go. It is in fact a PHP extension written in C mcdvoice login