Welcome Guest, Not a member yet? Register   Sign In
Config Static Variables Not Working
#1
Star 

Hi,
I am using a custom Config file. I populate its static variables by calling a method & then use those variables in BaseController. Its working fine in BaseController but when I try to use those same variables in AdminController extended from BaseController its variables show empty. I don't call the method to populate its variables in AdminController as its extended from BaseController. 
Following are details. Kindly help me understand why config file variables don't show any value in AdminController:
BaseController
PHP Code:
$this->config config('My_config');
// Following load_values() method loads users names & populates other static variables (eg: $users_array) of config class 'My_config'. I dont call this method again in AdminController
$this->config::load_values();  
echo $this->config::$users_array[0];  // Prints User Name 

AdminController extends BaseController

PHP Code:
$this->config config('My_config');
echo 
$this->config::$users_array[0]; // Empty 

$this->config::$users_array[0]; prints values in BaseController but don't print anything in AdminController. Please advise.
Regards
Reply
#2

(This post was last modified: 08-03-2021, 09:51 AM by paliz.)

look this my config 

PHP Code:
<?php namespace Modules\Common\Config;

use 
CodeIgniter\Config\BaseConfig;

class 
ModuleCommonConfig extends BaseConfig
{
    /**
    * --------------------------------------------------------------------
    * Default Core Site Config
    * --------------------------------------------------------------------
    *
    */

    /*
    *---------------------------------------------------------------
    * public directory
    *---------------------------------------------------------------

    */
    public $publicDirectory __DIR__ '/../../../public';
    /*
        *---------------------------------------------------------------
        * public directory
        *---------------------------------------------------------------

        */
    public $uploadDirectory __DIR__ '/../../../public/upload';

    /*
          *---------------------------------------------------------------
          * public directory
          *---------------------------------------------------------------

          */

    public $folderDirectory __DIR__ '../../../';

}



call config like that 
$customConfig 
= new ModuleCommonConfig(); 
Enlightenment  Is  Freedom
Reply
#3

config is mean for variable and value (for stand alone purpose) , others better use models or library
Reply
#4

Do not call `config` again in AdminController
Reply
#5

(08-03-2021, 07:20 PM)paulbalandan Wrote: Do not call `config` again in AdminController

Then how to access config in Admin Controller? 
I tried calling
   
PHP Code:
parent::$users_array[0]; 
and also tried
 
PHP Code:
$this->$users_array[0]; 
   but it resulted in error. 

As config is not an object, its just loaded and used & the variables are static so how I should access them in Admin Controller?
Reply
#6

What is the visibility of $this->config in BaseCOntroller? If set to private, AdminController can't access it. Needs to be at least protected or public.
Reply
#7

(08-03-2021, 09:36 PM)paulbalandan Wrote: What is the visibility of $this->config in BaseCOntroller? If set to private, AdminController can't access it. Needs to be at least protected or public.

I am just accessing it using
PHP Code:
$this->config config('My_config'); 
and then calling its method 
PHP Code:
$this->config::load_values(); 
to populate static variables of the config class.
As per documentation

// Get shared instance with config function
$config = config('Pager');
Reply
#8

Ok. I meant how you define it in your class?
PHP Code:
class BaseController
{
    protected 
$config# here what is the visibility you set for $config?

Reply
#9

(08-03-2021, 10:15 PM)paulbalandan Wrote: Ok. I meant how you define it in your class?
PHP Code:
class BaseController
{
    protected $config# here what is the visibility you set for $config?


I dont declare any variable like this. 
I think the real question is that I access a shared instance using 
$this->config = config('My_config'); 
and then populate its static variables like following:
$this->config::load_values();
Now as the variables in 'My_config' are static, they must maintain the values throughout the life of the process so when I access the same shared instance using
$this->config = config('My_config'); in another controller, it should have the static values intact. 
But actually they have empty values. Why?
Either $this->config = config('My_config'); creates a new instance & dont provide the shared instance or there is a way to access the static variables that I am unaware of.
Reply
#10

(This post was last modified: 08-04-2021, 02:42 AM by paulbalandan.)

I think I have a guess on the problem but needs some confirmation.
Can you share the code of your My_config?

Class objects are saved by reference by default so changing one should affect the other. I just need to confirm the structure of your config class to know the culprit behind this.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB