CodeIgniter Forums
Auto load Library before all Controllers - 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: Auto load Library before all Controllers (/showthread.php?tid=13743)



Auto load Library before all Controllers - El Forum - 12-03-2008

[eluser]Ty Bex[/eluser]
I would like to make a library available to all contollers.

My Library is in the application/libraries/Auth_user.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth_user{
    
    function Auth_user()
    {
        $CI =& get_instance();
        $CI->load->database();
        $CI->load->library('session');
        $CI->load->library('encrypt');
        $CI->load->library('validation');
        $CI->load->helper('form');
    }
    
    function check($check_level)
    {
      /*DO SOME CHEKCING*/
    }
}

Currently my controller is /application/controllers/backend/welcome.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();
        $this->load->library('auth_user');
        $this->auth_user->check('admin');
    }
    
    function index()
    {
        echo "<br />BACKEND WELCOME.PHP<br />SUCCESS TO INDEX FUNCTION<br /><br />";
    }
}


What I want to do is just call the library and not have to load the library for every page. (Commented out line)
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();
        /*$this->load->library('auth_user');*/
        $this->auth_user->check('admin');
    }
    
    function index()
    {
        echo "<br />BACKEND WELCOME.PHP<br />SUCCESS TO INDEX FUNCTION<br /><br />";
    }
}

I am not 100% where to go with this one...Thanks in advance


Auto load Library before all Controllers - El Forum - 12-03-2008

[eluser]Michael Wales[/eluser]
application/config/autoload.php


Auto load Library before all Controllers - El Forum - 12-04-2008

[eluser]xwero[/eluser]
Instead of loading the libraries and helper in your custom library you better add them to the autoload file too. This way if you change from authentication library your other code doesn't break.


Auto load Library before all Controllers - El Forum - 12-04-2008

[eluser]Ty Bex[/eluser]
Thanks so much for your help. I should never be coding after a 16 hour work day.
I was editing a backup of my autoload.php

xwero
Quote:Instead of loading the libraries and helper in your custom library you better add them to the autoload file too. This way if you change from authentication library your other code doesn’t break.

They are in my autoload.php but I have to $CI =& get_instance(); im my library.. Don't I??
Code:
$autoload['libraries'] = array('database', 'email', 'validation', 'session', 'encrypt', 'l_auth_user' );

$autoload['helper'] = array('url', 'form', 'date', 'security' );

application/libraries/L_auth_user.php
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class L_auth_user {
    
    function check($check_level)
    {
        $CI =& get_instance();
        $CI->load->database();
        $CI->load->library('session');
        $CI->load->library('encrypt');
        $CI->load->library('validation');
        $CI->load->helper('form');    

        /* DO SOME STUFF */
    }



Auto load Library before all Controllers - El Forum - 12-04-2008

[eluser]Rick Jolly[/eluser]
I think xwero means that you shouldn't load files only within your custom library if they are used in other parts of your application. You haven't done that, so you're ok.

I agree that you should load all files necessary within custom libraries regardless if they are loaded elsewhere. It doesn't hurt, since the CI loader will never load a file more than once. It prevents problems when someone removes a file from the autoload array.

For the record, I don't use autoload for that reason and because it isn't transparent: you can't look through the code to see which files are being loaded.