CodeIgniter,在库类中加载自定义配置的麻烦



我正在尝试在库类中加载自定义配置文件。我正在遇到配置值返回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');
    }
}

我得到了无效的响应。

任何帮助都将不胜感激。

测试代码后。我只发现一个问题仅仅是constructor的错字错误。你错过了拼写。因此,构造函数永远不会被调用。更改并检查。其他事情还不错

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

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

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'];
    }
}

/* 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'
);

最新更新