0

I'am probably missing some knowledges in PHP, and seems can't get it work right.

I have a application/config/cfg.backend.php with this values:

$config['head_meta']            = array(
    'stylesheets'               => array(
        'template.css'
    ),
    'scripts'                   => array(
        'plugins/jquery-2.0.3.min.js',
        'plugins/bootstrap.min.js'
    ),
    'end_scripts'               => array(
        'plugins/jquery-ui.js',
        'plugins/jquery.dataTables.min.js',
        'template.js'
    )
);

There I load all necessary scripts and css files, so whenever I will need to extend some of these arrays, I will simply use an array_push() function like I did in my application/controllers/backend/Categories.php:

class Categories extends Backend_Controller{

    function __construct(){

        parent::__construct();

        // Load dependencies
        $head_meta = config_item('head_meta');
        array_push($head_meta['end_scripts'], 'plugins/redactor.min.js', 'categories.js');
        array_push($head_meta['stylesheets'], 'redactor.css');
        var_dump($head_meta['end_scripts']);
    }

    // THE REST OF THE CLASS ...
}

So by doing a var_dump($head_meta['end_scripts']), I see that the array_push() did his job but my scripts weren't loaded, and I don't know why, I'am stucked here.

array (size=5)
  0 => string 'plugins/jquery-ui.js' (length=20)
  1 => string 'plugins/jquery.dataTables.min.js' (length=32)
  2 => string 'template.js' (length=11)
  3 => string 'plugins/redactor.min.js' (length=23)
  4 => string 'categories.js' (length=13)

Any suggestions what I'am doing wrong ?

==== UPDATED ====

I have a main template file located in applications/views/backend/templates/template.php where at the bottom of the page I do a foreach() to load end_scripts:

<?php foreach($this->config->item('end_scripts', 'head_meta') as $end_scripts):?>
    <script src="<?php echo base_url();?>assets/js/<?php echo $end_scripts;?>" type="text/javascript"></script>
<?php endforeach;?>

And to load a specific view into the main templates/template.php, I do this:

    // Insert catched data into the component view of main template
    $data['component'] = $this->load->view('backend/category_list', $componentData, TRUE);

    // Load a component view into the main template
    $this->load->view('backend/templates/template', $data);
4

1 回答 1

1

The Codeigniter Config class loads the config variables inside the class making them immutable without calling $this->config->set_item(). To add variables to an array in the config class from your controller, you need to modify the array, and then set the variables back to the config class before they can be accessed from elsewhere in the program.

class Categories extends Backend_Controller{

    function __construct(){

        parent::__construct();

        // Load dependencies
        $head_meta = config_item('head_meta');
        array_push($head_meta['end_scripts'], 'plugins/redactor.min.js', 'categories.js');
        array_push($head_meta['stylesheets'], 'redactor.css');
        $this->config->set_item('head_meta', $head_meta);
        var_dump($this->config->get_item('head_meta'));
    }

    // THE REST OF THE CLASS ...
}
于 2013-07-08T14:35:52.537 回答