Welcome Guest, Not a member yet? Register   Sign In
how to load config file in library
#1

[eluser]runrun[/eluser]
I got a config file asset.php

My config file is like so
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['enable_compress'] = FALSE;

I want to load it in a library called Asset

My library code is like so

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Asset {
    
    $CI =& get_instance();
    $CI->config->load('asset'); //loading asset.php config file
    
    function css()
    {
        $ai = $CI->config->item('enable_compress');
    }
}

?>

You can see I'm attempting to load the config file. Problem is it doesn't work, it's showing me a blank webpage when I run this library.
#2

[eluser]Vheissu[/eluser]
The issue is that you're trying to store a copy of the CI super object as a class variable which will not work. You need to store it inside of a class constructor. See below for code that will work Smile I've also used a helper function for accessing config items easier below.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Asset {

    private $CI;

    public function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->config->load('asset');
    }
    
    function css()
    {
        $ai = config_item('enable_compress');
    }
}

?>




Theme © iAndrew 2016 - Forum software by © MyBB