CodeIgniter Forums
[answered] is this the correct way to get info from config file ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: [answered] is this the correct way to get info from config file ? (/showthread.php?tid=19273)



[answered] is this the correct way to get info from config file ? - El Forum - 06-02-2009

[eluser]jozeunico[/eluser]
I am creating my own library, but I have a couple of doubts, the most important is how can I retrieve the values (inside the library to display them) from the config file.

I did something like this:

I have my config file "mylibrary.php" and of course my library "Mylibrary.php"

My library:
Code:
class Mylibrary {

var $title;
    
    function Mylibrary()
    {

        $this->CI=& get_instance();
        $this->title = $this->CI->config->item('title');
        
    }
    
    function showMessage(){
        echo $this->title;        
    }
}

My config file just have this:

Code:
config['title']='My title';

In a controller a load the library and call the funcion "showMessage"

and it works, my doubt it's if it's correct use this:
Code:
$this->CI=& get_instance();
$this->title = $this->CI->config->item('title');

To set values from a config file and display them (or use them for any operation).

Thanks.

p.s. I saw this in the FreakAuth library just if you are wondering.


[answered] is this the correct way to get info from config file ? - El Forum - 06-02-2009

[eluser]omar-303[/eluser]
I think you should load the configuration file first

Code:
$this->CI =& get_instance();
$this->CI->load->config('mylibrary');
$this->title = $this->CI->config->item('title');



[answered] is this the correct way to get info from config file ? - El Forum - 06-02-2009

[eluser]Wilbur Himself[/eluser]
config_item('item_key')

The Config library is the preferred way of accessing configuration information, however config_item() can be used to retrieve single keys. See Config library documentation for more information.

http://ellislab.com/codeigniter/user-guide/general/common_functions.html


[answered] is this the correct way to get info from config file ? - El Forum - 06-02-2009

[eluser]Thorpe Obazee[/eluser]
[quote author="jozeunico" date="1244005441"]
Code:
$this->CI=& get_instance();
$this->title = $this->CI->config->item('title');

To set values from a config file and display them (or use them for any operation).

Thanks.

p.s. I saw this in the FreakAuth library just if you are wondering.[/quote]

I believe it is. Of course the config file should have been loaded already.


[answered] is this the correct way to get info from config file ? - El Forum - 06-02-2009

[eluser]jozeunico[/eluser]
[quote author="omar-303" date="1244007123"]I think you should load the configuration file first

Code:
$this->CI =& get_instance();
$this->CI->load->config('mylibrary');
$this->title = $this->CI->config->item('title');
[/quote]

Quote:I believe it is. Of course the config file should have been loaded already.

I'm sorry y forgot to say it, I've already loaded the config using de /config/autoload.php

onfig_item(‘item_key’)
Quote:The Config library is the preferred way of accessing configuration information, however config_item() can be used to retrieve single keys. See Config library documentation for more information.

http://ellislab.com/codeigniter/user-guide/general/common_functions.html
I didn't understand the doc (my english isn't good), thats why the post.

Thanks anyone and if somebody can explain these lines (for dummys) I'll thank you.
[code]
$this->CI =& get_instance();
$this->CI->load->config('mylibrary');

[/code


[answered] is this the correct way to get info from config file ? - El Forum - 06-03-2009

[eluser]omar-303[/eluser]
Code:
$this->CI =& get_instance();
You are accessing the Codeigniter Object by storing it(by reference) in the $this->CI variable

Code:
$this->CI->load->config('mylibrary');
Using the loader class you load the mylibrary.php configuration file


[answered] is this the correct way to get info from config file ? - El Forum - 06-03-2009

[eluser]jozeunico[/eluser]
[quote author="omar-303" date="1244076278"]
Code:
$this->CI =& get_instance();
You are accessing the Codeigniter Object by storing it(by reference) in the $this->CI variable

Code:
$this->CI->load->config('mylibrary');
Using the loader class you load the mylibrary.php configuration file[/quote]

Thank you, I guess I got it, but still get confused because it use a lot of "levels" or the symbol "->", I mean, I understand the "$controller->function()" notation but when I see more "->" I just get lost, thank you again.