2017-06-28 4 views
1

Ich versuche, eine benutzerdefinierte Konfigurationsdatei in einer Bibliotheksklasse zu laden. Ich habe Probleme, bei denen die Konfigurationswerte null zurückgeben.CodeIgniter, Problem beim Laden einer benutzerdefinierten Konfiguration in einer Bibliotheksklasse

config file: 'couriers.php'

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

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

    } 
} 

Ich bin ein NULL-Antwort zu erhalten.

Jede Hilfe wird geschätzt.

Antwort

0

Nach dem Testen Ihres Codes. Ich habe nur ein Problem gefunden, das ist einfach Tippfehler für constructor. Du vermisst es buchstabiert. So wird der Konstruktor nie aufgerufen. Ändern Sie es und überprüfen Sie es. Andere Dinge sind gut

public function __construct() { 

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

/* Klasse */

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' 
); 
0

Sie haben Konfiguration zu laden und es auf ein Objekt zu zeigen und verwenden, während die Rendite:

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

    } 
} 
Verwandte Themen