CodeIgniter shared data between calls to load->view
Consider a view called render_thing, which I load from a controller like so:
$html = $this->load->view(
'render_thing',
array(
'someParam' => $globalParam
'permissionMode' => 'guest'
),
true
);
log($html);
Later on in that same controller, I load the view again, except I don't override the optional permissionMode parameter. I'm assume that in the view code, $permissionMode
would be unset
.
$moreHtml = $this->load->view(
'render_thing',
ar开发者_开发问答ray(
'someParam' => 'blablabla'
),
true
);
However, in the render_thing view code, on the second call, $permissionMode
is still 'guest'
. Can you tell me what is going on here?
Thanks!!!
From Loader.php, Loader::_ci_load
in the CodeIgniter source...
/*
* Extract and cache variables
*
* 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.
*/
if (is_array($_ci_vars))
{
$this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
}
extract($this->_ci_cached_vars);
So, this would be why the parameter is still set. load_vars
is not a method, but vars
is; problem is that it doesn't provide a facility to erase the cache. Therefore, since CodeIgniter is still PHP4 compatible, you may always do this: $this->load->_ci_cached_vars = array();
.
I've had the same problem and figured out the problem in Loader.php as follows;
/*
* Extract and cache variables
*
* 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.
*/
if (is_array($_ci_vars))
{
$this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
}else{
$this->load->_ci_cached_vars = array();
}
extract($this->_ci_cached_vars);
精彩评论