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

CodeIgniter, trouble loading a custom config in a library class

我正在尝试在库 class 中加载自定义配置文件。我 运行 遇到配置值返回 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 响应。

感谢任何帮助。

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

    }
}

/* 配置 (couriers.php) */

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

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

您必须加载配置并将其指向一个对象并在 return:

时使用它
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'];

    }
}

测试您的代码后。我发现只有一个问题是 constructor 的拼写错误。你错过了拼写。所以构造函数永远不会被调用。更改并检查。其他都还好

public function __construct() {

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