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

index.volt way to store var whole entire views

Hello! I've started learn Phalcon few days ago and stack in one small problem. I build index and user MVC, where index contain whole view with content() block and user is just auth/login/logout functions. All controllers generated content (except top index) shows in this block. But I need to use $user->nickname in whole pages topmenu and want DRY. What I shall do to share variable without copypaste $this->view->user = Users::findFirstByName('I am') into all actions? Thank you very much!

Upd: maybe there is some way to share var from index controller to all views inside, but I didn't find this way



11.6k

take a look at the INVO demo, in sessionController.php, you will see how user's datas are stored in php global session variables



79.0k
Accepted
answer
edited May '16

You can put your global logic into onConstruct() method in your BaseController. Afterwards, $user->nickname would become available and filled out on every subcontroller or any action call. Then you need to set your Volt / views accordingly.

I forgot to use session vars, my fault. But for now I used global var in basecontroller. Thank you, friends!

take a look at the INVO demo, in sessionController.php, you will see how user's datas are stored in php global session variables