Question

I have a viewer helper function that loads the main content alongside the footer/header. The bug/unexpected behavior occurred when I loaded the array's key for the header that shares the same name for a variable in the main content view - the same array is loaded for both the header and main content.

I thought it's normal, since the same $data array was sent to the header and main content as-well(as mentioned before). So the variable will naturally be present in both views. But, well, it wasn't exactly like that. I unset the $data variable after sending the data to the header then re-created it when I wanted to send some data to the main view - but still the problem is not fixed.

I made a simple example for this bug/unexpected behavior:

Consider this view, named test:

<?php 
echo $some_data;  

And this controller:

class Test extends CI_Controller {

    function index() {

    $data['some_data'] = 'Some data.';
    $this->load->view('test', $data);
    /* 
     * Output:
     * Some data.
     */
    unset($data);
    unset($data['some_data']);//Just to make sure it's not PHP's fault.
    $this->load->view('test');
    /*
     * Output:
     * Some data.
     * 
     * Even though the $data variable is unsetted AND not passed!
     */

    $different_data = array();

    $this->load->view('test', $different_data);     
    /*
     * Output:
     * Some Data.
     * 
     * Still outputs the same thing, even though
     * I'm sending different array(and the $data array is unstted).
     * 
     */ 

    }
}  

Note: The whole code will output Some data. three times.

The only way to solve this issue is sending a different array and setting the array key(which is some_data) to something else which will override the old one.

So, is this a bug or something made by CodeIgniter's dudes?

Était-ce utile?

La solution

This is expected behavior.

Once variables are set they become available within the controller class and its view files. Sending an array in $this->load->view() is the same as sending an array directly to $this->load->vars() before calling the view file. This simplifies things for most people using multiple views in a controller. If you are using multiple view files in a single controller and want them to each have their own set of variables exclusively, you'll need to manually clear out the $this->load->_ci_cached_vars array between view calls.

A code comment in the Loader class describes another situation showing why this is the desired default behavior:

//You can either set variables using the dedicated $this->load_vars()
//function or via the second parameter of this function. We'll merge
//the two types and cache them so that views that are embedded within
//other views can have access to these variables.

Autres conseils

we had the same problem like you and our Alien coworker found THE solution:

if file: codeigniter\system\core\Loader.php

find the code: (i think the line number is 806):

$this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);

and correct it to:

$this->_ci_cached_vars = $_ci_vars;

best regards

This is a CodeIgniter issue. The variables you are sending in seems to be cached until you override them. I have encountered this myself and can verify it.

$this->load->view('test', array('some_data' => NULL));

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top