CodeIgniter在对load->视图的调用之间共享数据

时间:2011-02-23 23:28:46

标签: php codeigniter

考虑一个名为render_thing的视图,我从控制器加载,如下所示:

$html = $this->load->view(
  'render_thing',                   
  array(
    'someParam' => $globalParam
    'permissionMode' => 'guest'
  ),
  true
);
log($html);

稍后在同一个控制器中,我再次加载视图,除了我没有覆盖可选的permissionMode参数。我假设在视图代码中,$permissionMode将为unset

$moreHtml = $this->load->view(
  'render_thing',                   
  array(
    'someParam' => 'blablabla'
  ),
  true
);

但是,在render_thing视图代码中,在第二次调用时,$permissionMode仍为'guest'。你能告诉我这里发生了什么吗?

感谢!!!

2 个答案:

答案 0 :(得分:2)

来自Loader.php,CodeIgniter来源中的Loader::_ci_load ......

    /*
     * 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);

所以,这就是为什么参数仍然设置的原因。 load_vars不是方法,但vars是;问题是它没有提供擦除缓存的工具。因此,由于CodeIgniter仍然兼容PHP4,因此您可以始终执行此操作:$this->load->_ci_cached_vars = array();

答案 1 :(得分:1)

我遇到了同样的问题,并在Loader.php中发现了如下问题;

/*
* 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);