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

Getting (and sending to views) session variable from BaseController

Hello Dear,

Instead of always doing the below

public function allAction()
{
    $alls = $this->session->get("sessvar");
    $this->view->allsvar = $alls; 
}

...in all my methods, I tried the below

<?php

use Phalcon\Mvc\Controller;

class BaseController extends Controller
{

    public function initialize()
    {
        $svariable = $this->session->get("sessvar");
    $this->view->svar = $svariable; 
    }

}

and it worked!!!!....ie I tried to always inherit it from my base controller after initialization of same and all my views began to see $svar

Question: Is there any security or other vulnerability to this?

(would be so glad if "Baba"-Guti comments on this)

If your variable is needed all over the application, then, setting it in BaseController is the right way to do it. No point in passing the variable to view on each action. The end result will be the same.



125.8k
Accepted
answer

Doing this action in the BaseController class is no more or less secure than doing it in each individual action/controller.