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

SessionBags only in Controller available

Refering to https://docs.phalcon.io/en/latest/reference/session.html#session-bags. I wanted to store data via SessionBag but when i want to access the data from another controller (which was called after the first one) data is gone. To give you a better point of view:

<?php
// SessionController.php:
class SessionController extends BaseController {
  // some code ...
  public function loginAction() {
      $this->persistent->foo = "bar";
      $this->flash->notice($this->persistent->foo);
      // some code ... like loading a form

      return $this->response->redirect();
  }
}
// IndexController.php:
class IndexController extends BaseController {
  public function indexAction(){
      $this->flash->notice($this->persistent->foo); // does not work
  }
}

Am I doing something wrong or did I misunderstood the documentation?

EDIT: Changed example to a more accurate one.



1.1k
Accepted
answer

Hey there. According to the documentation, persistent bags are available only in your current class.

The data added to the session ($this->session) are available throughout the application, while persistent ($this->persistent) can only be accessed in the scope of the current class.

Right above the title



1.7k
edited Aug '14

The data added to the session ($this->session) are available throughout the application, while persistent ($this->persistent) can only be accessed in the scope of the current class.

Just use $this->session instead of $this->persistent


well, a step slow.



766

Thought that it's just working in the current class. I should've read the docs more carefully ...

Thanks for the answers.