Ethan
Ethan

Reputation: 2784

Creating Config File for Custom Library in CodeIgniter

I've looked through all of the CI documentation, and done some Googling of it, but I still can't quite seem to figure out how to create a configuration file for a custom library in codeigniter. If somebody could even just point me in the direction of where in the docs I could find my answer it would be greatly appreciated.

I am creating a library in CI that makes use of several database columns that can vary in name between applications, so I would like the names to be stored in a custom config file. Then I would like to be able to load these values in the construct of the library.

So my two questions are:

1.) How do I name the config file, and how do I name variables within that file so they don't overwrite any other config vars?

2.) How do I get the values from within my library?

Upvotes: 1

Views: 2435

Answers (3)

Joe Cheng
Joe Cheng

Reputation: 9524

in your config /config/your_conf.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config = Array(
        'your_conf1' => 'your_conf_val1',
        'your_conf2' => 'your_conf_val2',
        'your_conf3' => 'your_conf_val3'
    );

in your controller:

$this->config->load('your_conf');
var_dump((array)$this->config); //show all the configs including those in the your_conf.php

Upvotes: 0

Gras Double
Gras Double

Reputation: 16373

If there is a config/libraryname.php file, it will be automatically loaded, just before library instanciation.

(so, beware of name conflicts with CI's config files)


Side note: this autoloading is disabled if you pass an array as the 2nd argument:

$this->load->library('thelibrary', array('param1' => 'value1'));

Upvotes: 1

dm03514
dm03514

Reputation: 55952

When i have questions like this i like to look at other projects that already do this. We utilized Tank_auth in almost all of our ci projects. This is a popular authentication library, which has its own custome config files

  1. It just creates its own config file in application/config directory. You could prefix your config items with your app name to ensure that they are unique

  2. it then just loads it in the constructor:

    $this->ci->load->config('tank_auth', TRUE);
    

Upvotes: 4

Related Questions