Welcome Guest, Not a member yet? Register   Sign In
using a library in combination with a config file.
#1

[eluser]Bramme[/eluser]
Hey all,

I'm using my own Auth library, so far I've been using it for only one controller at a time, but now I'm starting on my first multi controller application (what a challenge :p). Anyhoo, I need my Auth library for all those controllers and instead of using $this->auth->initialize($config) in all of my controllers, I would like to use a config file. But how do I link my config file to my library so that it gets automatically loaded? I was looking through some other libraries, but couldn't find it directly.
#2

[eluser]xwero[/eluser]
in the Loader class there is a function _ci_init_class and it contains following code
Code:
// 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);
            }
        }
So if there is a config file with the same name as the library the config file will be loaded. And then it is up to you to read the config settings in the library methods.
#3

[eluser]Bramme[/eluser]
ooh, it happens automatically (the including of the config) hmm. That's interesting. But how does, say, the pagination class does this? I mean, I use a separate config array, but I just can't find where they load their config items in the library...
#4

[eluser]xwero[/eluser]
If the config file is loaded it will be added as a parameter to the class. from the same method
Code:
if ($config !== NULL)
        {
            $CI->$classvar = new $name($config);
        }
        else
        {
            $CI->$classvar = new $name;
        }
For this behavior to work you need an initialize method in your library.
#5

[eluser]Bramme[/eluser]
Aaaah, I get it! Thanks a bunch xwero for your help. I can't keep up with how many times you've helped me figure out smth so far.
Code:
function Auth($props = array())
    {
        $this->CI =& get_instance();
        
        // Load additional libraries, helpers, etc.
        $this->CI->load->library('session');
        $this->CI->load->database();
        $this->CI->load->helper('url');
        
        if (count($props) > 0)
        {
            $this->initialize($props);
        }
    }
    
    /**
     * initialize class preferences
     *
     * @access    public
     * @param    array
     * @return    void
     */    
    function initialize($props = array())
    {
        if (count($props) > 0)
        {
            foreach ($props as $key => $val)
            {
                $this->$key = $val;
            }
        }    
    }
There we are ^^




Theme © iAndrew 2016 - Forum software by © MyBB