• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[answered] is this the correct way to get info from config file ?

#1
[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.

#2
[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');

#3
[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

#4
[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.

#5
[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-gui...tions.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

#6
[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

#7
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.