Welcome Guest, Not a member yet? Register   Sign In
Unable to access variable
#1

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.
Reply
#2

The local variable $dh_site_theme is not defined in your code.
Why do you think it has a value?
Reply
#3

start learning PHP =)
Reply
#4

(This post was last modified: 03-26-2022, 09:41 PM by spreaderman.)

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.
Reply
#5

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
Reply
#6

(This post was last modified: 03-27-2022, 01:33 AM by spreaderman.)

@ 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()
    {
        $globalSettings = cache('global_settings');
        if (null === $globalSettings) {
            $globalSettings = cache('global_settings');
            $appSettingModel = model('\App\Models\AppSettingModel');
            $app_settings = $appSettingModel->getAppSettings();
            $globalSettings = [];
            foreach ($app_settings as $row) {
                $globalSettings[$row->app_setting_key] = $row->app_setting_value;
            }
            cache()->save('global_settings', $globalSettings, DAY*15);
        }
        return $globalSettings;
    }
(2) and then in the PublicController's __construct, call it like:

Code:
class Public_Controller extends BaseController{
        protected function __construct(){
        $site_settings = $this->loadGlobalSettings() ;
}

Something like above is ok?
Reply
#7

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);
}
}
Reply
#8

Thank you very much. All very clear. Much appreciated!!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB