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

Persistent data not available in Micro app?

Hello, Using this from BaseConroller:

$this->persistent->myvar = $audit->getWebserviceAuditID();

All other controllers can access this variable then. like:

$this->persistent->myvar

Is there a way to access this variable from main index.php front controller - i.e. which is defined as a such:

class Micro extends \Phalcon\Mvc\Micro

This does not work:

echo "Welcome, ", $app->persistent->myvar;

When I dump $app->persistent, I see there is an object with variable filled with value:

["persistent"]=> object(Phalcon\Session\Bag)#66 (5) { ["_dependencyInjector":protected]=> RECURSION ["_name":protected]=> string(31) "Com\MycategoryController" ["_data":protected]=> array(1) { ["myvar"]=> string(3) "981" } ["_initialized":protected]=> bool(true) ["_session":protected]=> object(Phalcon\Session\Adapter\Files)#67 (3) { ["_uniqueId":protected]=> NULL ["_started":protected]=> bool(false) ["_options":protected]=> NULL } }

As we see, there is my data:

["_data":protected]=> array(1) { ["myvar"]=> string(3) "981" }

I just cannot figure it out how to access this value?!

Dump here returns NULL for both:

var_dump($app->persistent->myvar, $app->persistent['myvar']);

Persistent data can be accesed only in the same class(extended class) so you cant access it in micro class. You have to use sessions or something else(just same cache). ALSO micro apps shouldnt really have controllers, cuz then they are not really micro.

That's what I thought.

From official Phalcon documentation:

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.

Thanks for the clarification!

Gonna have to figure out why my session bag isn't working either outsite the controllers...

P.S. Yes, micro should have all-in-one logic, but this way I extended business logic easier.

edited Nov '15

This way it works.

Well, it's one page app anyway (really micro app). But persistent data does work with Micro, that's important to note.

$app = new Phalcon\Mvc\Micro($di);

$app->before(function () use ($app) {
//some app logic, database calls, etc.
  $id = $app->db->lastInsertId();
  $app->persistent->myID = $id;
}

$app->after(function () use ($app) {
echo $app->persistent->myID; //prints string("1923")
}