Welcome Guest, Not a member yet? Register   Sign In
Error when accessing base controller variable
#1

Hi there, I'm hoping someone can help. I've got a class that I'm using in my controllers and libraries to handle settings that are stored in the database. When accessing them in my controller via $this->settings->get('blah'), I have no problems at all.

However, when I pass $this->settings in the controller's __construct() to my template library, it's passing a null value and I get this error:

Quote:TypeError

Argument 1 passed to MyApp\TemplateRenderer::__construct() must be an instance of MyApp\Settings, null given, called in



I've loaded the library in the base controller as shown below.

This is my BaseController:

PHP Code:
    public $session$settings;

    
/**
     * Constructor.
     */
    
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);

        
//--------------------------------------------------------------------
        // Preload any models, libraries, etc, here.
        //--------------------------------------------------------------------
        $this->session session();
        $this->settings = new Settings();
    } 


This is the Settings class:

PHP Code:
class Settings {
    
    
// A container for all of the settings values.
    protected $settings = [];

    public function __construct()
    {
        $settings_model = new SettingsModel();

        $settings $settings_model->findAll();

        foreach($settings as $setting) {
            if($setting['value'] == 'true' || $setting['value'] == 'false') {
                $this->settings[$setting['key']] = ($setting['value'] == 'true' true false);
            } else {
                $this->settings[$setting['key']] = $setting['value'];
            }
        }
    }

    public function get($key)
    {
        if(key_exists($key$this->settings)) {
            return $this->settings[$key];
        } else {
            return false;
        }
    

And finally my controller itself:

PHP Code:
class AccountController extends BaseController
{
    public function __construct()
    {
        $this->template = new TemplateRenderer($this->settings);
    

Any help would be greatly appreciated! Thanks.
WebDevZone - A new, friendly web development community
Reply
#2

this is how i'm setting some BaseController , class member properties

public function __construct()
{
helper(['text', 'date']);

$this->theTime = now('Australia/Victoria');
$this->theDate = date("d/m/Y",$this->theTime);
}
Reply
#3

Your issue is the class constructor will run before initController method. Use this instead.

PHP Code:
class AccountController extends BaseController
{
    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);

        $this->template = new TemplateRenderer($this->settings);
    
Reply




Theme © iAndrew 2016 - Forum software by © MyBB