Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.



50.6k
Accepted
answer

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



44.7k

You can extend the View class to pass in any universal constants or global values on its instantation (with constructor) and then your approach in the controller is good for values that are changing or are security sensitive.