Unable to access variable |
I have a BaseController which initiates some variables as follows;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); $this->global_settings = $this->loadGlobalSettings(); $global_settings_cache = cache()->get('global_settings'); $this->data['dh_site_theme'] = $global_settings_cache['dh_site_theme']; } I then have UserPublic_Controller as follows: namespace App\Controllers\User_public; use App\Controllers\User_Public\UserPublic_Controller; use App\Controllers\BaseController; use App\Libraries\Theme; class UserPublic_Controller extends BaseController{ protected function __construct(){ $this->data['user_public_css'] .= $dh_site_theme->theme_cdn($dh_site_theme); } I cannot understand why I cannot access $dh_site_theme ... it shows null. Even in the VIEW debug bar I can see it is set to "journal", one of the seems I have. VIEW DATA / Debug bar. dh_site_theme journal Any help appreciated.
The local variable $dh_site_theme is not defined in your code.
Why do you think it has a value?
This is defined in the basecontroller; $this->data['dh_site_theme'] = $global_settings_cache['dh_site_theme'];
$data is defined as: protected $data = []; in the BaseController. UserPublic_Controller extends BaseController To access $data should be $this->data['dh_site_theme'] I thought (sorry, copied the wrong script below) but it turns out null.
Hi
it happens because constructor of UserPublic_Controller class called before initController of BaseController executed. or in other words first the constructor is called and then the function initController inherited from BaseController
@ vitnibel thanks. Did not realize that. I thought initController was more or less working as a __constructor. Is there any way to add a __constructor to initController? I am creating my cache in the BaseController which is a bunch of settings for my website. They are mainly required in the view so that works ok but I create my CSS, JS, etc, in the __construct part of my Public_Controller and need to know the cache setting then. I would like to keep the reading of cache in the base controller and have it loaded it first. Any ideas?
I guess one solution is (1) insert below method into initController Code: public function loadGlobalSettings() Code: class Public_Controller extends BaseController{ Something like above is ok?
everything is easier.
you just need to place the initialization code of the UserPublic_Controller class in the initController function and call the base class function of the same name in the first line. class Public_Controller extends BaseController { public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); $this->data['user_public_css'] .= $dh_site_theme->theme_cdn($dh_site_theme); } }
|
Welcome Guest, Not a member yet? Register Sign In |