1

So I was reading in the Codeigniter user guide and they say that:

You can also pass parameters stored in a config file. Simply create a config file named identically to the class file name and store it in your application/config/ folder. Note that if you dynamically pass parameters as described above, the config file option will not be available.

I have a config file set up with the identical name as my library and I am wondering how I can access the contents of that file. Do I need to load the config before loading the lib? Do all of the variables need to be elements of an array?

examples:

-- Config file --

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$var1 = "Variable 1";
$var2['name'] = "foo";
$var2['title']= "bar";
$var2['content'] = "Lorem ipsm dolar sit imut";

-- Library file --

class Library 
{
    var $data;
    function __construct($data)
    {
            $this->data = $data;
    }
}

-- Then something along the lines of --

function __construct() {
    parent::__construct();
    $this->load->library("library");
}
function index()
{
    var_dump($this->library->data);
}

(Yes I figure the name Library is reserved but this is an example)

Shouldn't the var_dump simply dump the contents of the config file? What am I doing wrong?

2
  • Try getting the config data with $this->config->item('name'); - And i think you need arrays. So $var = "variable"; won't work. Commented Apr 4, 2014 at 20:47
  • So you're speaking in terms of outside of the library, load the config and retrieve the variables? Commented Apr 4, 2014 at 21:23

2 Answers 2

2

Few things to make this work :

Your classname FILE should start with a capital letter, Your config array must be defined as $config = array() (and not $var or any other name), Your config file must be lowercase.

The config file is passed through the constructor, so class($config=array()), you can then access and store the config variables on an instance variable.

hope it helps:

class Foo // Foo.php
{
    function __construct($config=array())
    {
        $this->config = $config;
    }

} //end of class

config file:

$config['url'] = 'stuff'; // config file : foo.php

good luck :)

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I was trying but for some reason the config wasn't being passed.
Did a print_r on the $config var, I found that it was an array with an array called (in this example) 'Foo'. So to access the config vars, you would do something along the lines of $this->config = $config['foo']; and that would be the config for the class Foo... if all of that abstraction makes sense :)
0

To access the variables of config file in your custom library, you can initialize a new CI instance and access the content of config like below:

$this->ci = & get_instance();

$configs = $this->ci->config;

Now do var_dump($configs) to see content of config file.

2 Comments

Ended up having to go with this option since my config wasn't getting automatically passed and I didn't wanna end up spending hours troubleshooting.
fair enough, You are binding your class very tightly with CI though, and loose coupling is normally better. The code I placed below works perfectly on my local host, When you get around to it make sure you trouble shoot it, I think it's important enough to invest your time on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.