EllisLab text mark
Advanced Search
     
Linking a config with a class
Posted: 17 October 2008 09:54 AM   [ Ignore ]
Joined: 2008-10-17
2 posts

Hello,

I want to have a config linked with a particular library, so that when I load the library in my controller, the library class’s constructor pulls its parameters from the config file.

From the docs:

You can also pass parameters stored in a config file. Simply create a config file named identically to the class file name and store it in your application/config/ folder.

If my class is as follows:

class LDAP {

    
function LDAP($params)
    
{
        
// Do something with $params
        
print_r($params);
        
// Print the default user, for example
        
echo $params['default']['user'];
    
}

What should my config/LDAP.php look like? Is my constructor correct here?

 
Posted: 17 October 2008 10:21 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-06
918 posts

a few thoughts… some probably irrelevant:

post your class instantiation code. according to the docs you posted the config option won’t work if you try to also pass parameters. just make sure you aren’t doing that.

capitalization? i don’t know all the vagaries of capitalization on all PHP versions, CI versions, and OSes but you might try some variations using ucfirst() style capitalization for the various parts of your class init code.

what does it do when you pass the config array explicitly to the constructor rather than relying on CI to do it behind the scenes from the config file?

EDIT: oh yeah, i forgot to say welcome to CI!!

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

 
Posted: 17 October 2008 10:49 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2008-10-17
2 posts
sophistry - 17 October 2008 02:21 PM

post your class instantiation code. according to the docs you posted the config option won’t work if you try to also pass parameters. just make sure you aren’t doing that.

The instantiation call was correct, and I had read that part about how dynamically passed parameters will clobber any config file parameters, but…

sophistry - 17 October 2008 02:21 PM

capitalization? i don’t know all the vagaries of capitalization on all PHP versions, CI versions, and OSes but you might try some variations using ucfirst() style capitalization for the various parts of your class init code.

Woah, good call! In CI’s libraries/Loader.php:

$class strtolower($class);

// Is there an associated config file for this class?
if ($config === NULL)
{
    
if (file_exists(APPPATH.'config/'.$class.EXT))
    
{
        
include_once(APPPATH.'config/'.$class.EXT);
     
}

Looks like the class name gets strtolower()‘d before CI looks for a config file. In this case, I named my config file “ldap.php” and it worked:

$config['ldap']['host']   'localhost';
$config['ldap']['user']   'myuser';
$config['ldap']['pass']   'mypass';
$config['ldap']['basedn''dc=example,dc=com'
sophistry - 17 October 2008 02:21 PM

EDIT: oh yeah, i forgot to say welcome to CI!!

Thanks!

 
Posted: 23 February 2009 10:53 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2009-02-23
14 posts

I struggled with this for a bit as well. what the documentation doesn’t mention is that the file config/your_class_name.php should contain an init object assigned to a var called $config:

your class

class My_funky_library
{    
    
function My_funky_library$params )
    
{
        var_dump
$params );
    

(in this file, it doesn’t matter whether you use $params, $config, or a valid variable name of your choice

your config file, in config/my_funky_library.php:

$config = array(
   
'setting1' => 123,
   
'setting2' => 455
//etc
); 

the variable here MUST be called $config for it to work.

Not obvious if you are new to CI.

cheers
fritz

 
Posted: 23 February 2009 11:58 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-06
918 posts

hi fritz,

welcome to CI…

i checked the config lib docs and you are right! the variable has to be named $config for CI to load it properly. that is a big omission in the docs IMHO.

i suggest that you post this to the bug tracker as a ‘Typo’ in the manual - there’s a category for those because the CI devs want the manual to be great.

cheers.

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter