开发者

Codeigniter return config file as array with autoload enabled

So I'm using CodeIgniter to build a website and I've made it so that all my specific settings are stored in a config file that's automatically loaded. I've also built a page that loads the settings file, makes a nice little table and allows me to edit everything from that page, afterwards it saves the entire page again (I know I could've done the same with a database but I want to try it this way).

My problem is that I can't seem to use this bit when autoloading of my config file is enabled, but when I disable autoloading I can't seem to manually load it, it never finds my variables. So what I'm doing here is just taking all values from the config file and putting them in a single array so I can pass this array onto my settings administration page (edit/show all settings).

$this->config->load('site_settings', TRUE);
$data['settings'] = $this->config->item('site_settings');
...
$this->load->view('template', $data);

config/site_settings.php

 <?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    $config['header_img'] = './img/header/';
    $config['copyright_text'] = 'Copyright Instituto Kabu';
    $config['copyright_font'] = './system/fonts/motoroil.tt开发者_如何学Cf';
    $config['copyright_font_color'] = 'ffffff';
    $config['copyright_font_size'] = '32';


Is the file content definitely correct? (Check braces, quotes...)

IS the config file name the same as the one you're requesting?


Aren't you are trying to fetch an item that does not exist, so it just returns FALSE (boolean)? You are trying to load the item "site_settings", but you have no item with this name in the array index. Instead it should be, for example when loading a single item, $this->config->item('header_img');

Also with your 2nd parameter when loading it, each config file will be stored in an array index corresponding to the name of the config file.

$this->config['site_settings'].

Read more about the config class and it's usage here


Ok, I've found the solution. The problem was this check built into the Config library:

if (in_array($file, $this->is_loaded, TRUE))
{
    return TRUE;
}

So I edited it with an extra parameter to look like this:

function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE, $skip_loadedCheck = FALSE)
    {
        ...

                if(!$skip_loadedCheck) {
                    if (in_array($file, $this->is_loaded, TRUE))
                    {
                            return TRUE;
                    }
                }

Now, to load all the data in an array I can just do this:

$this->config->load('site_settings', TRUE, FALSE, TRUE);
$data['settings'] = $this->config->item('site_settings');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜