CodeIgniter,在库类中加载自定义配置时遇到问题

时间:2017-06-28 12:05:39

标签: php codeigniter config codeigniter-3

我正在尝试在库类中加载自定义配置文件。我遇到的问题是配置值返回null。

配置文件:' couriers.php'

$config['ups'] = 'some keys';

库文件:' /library/Track/Ups.php'

class Ups {

    public $ci;

    public function __contruct() {

        $this->ci =& get_instance();
        $this->ci->config->load('couriers');
    }

    public function GetUPSKey() {

       return config_item('ups');

    }
}

我收到了NULL响应。

感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

测试代码后。我发现只有一个问题只是var activities = realm.All<ActivityType>().ToArray(); var ids = new HashSet<string>(data.Select(d => d.id)); realm.Write(() => { foreach (var activity in activities) { if (!ids.Contains(activity.Id)) { realm.Remove(activity); } } }); 的错字错误。你错过拼写它。所以构造函数永远不会被调用。改变它并检查。其他的事情很好

constructor

答案 1 :(得分:0)

/ * Class * /

class Ups {

    protected $ci;
    protected $config;

    public function __construct() {

        $this->ci =& get_instance();

        // Loads a config file named couriers.php and assigns it to an index named "couriers"
        $this->ci->config->load('couriers', TRUE);

        // Retrieve a config item named ups contained within the couriers array
        $this->config = $this->ci->config->item('ups', 'couriers');
    }

    public function GetUPSKey() {

       return $this->config['key'];

    }
}

/ * Config(couriers.php)* /

$config['ups'] = array(
    'key' => 'thisismykey',
    'setting2' => 'etc'
);

// Additional Couriers etc
$config['dhl'] = array(
    'key' => 'thisismykey',
    'setting2' => 'etc'
);

答案 2 :(得分:0)

您必须加载配置并将其指向一个对象,并在返回时使用它:

class Ups {

    public $ci;

    public function __contruct() {

        $this->ci =& get_instance();
        $this->couriers_config = $this->ci->config->load('couriers');
    }

    public function GetUPSKey() {

       return $this->couriers_config['ups'];

    }
}