在库中加载视图,缓存的vars问题 [英] Loading view inside a Library, issues with cached vars

查看:125
本文介绍了在库中加载视图,缓存的vars问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用load-> view实现小部件库.我知道我可以使用include直接调用文件并避免vars缓存问题,但是只是想知道为什么它不起作用.

I am trying to implement a widgets library using load->view. I know I can use include to call directly the file and avoid the vars cache issues but just wondering why it does not work.

这是我的代码结构:

我的控制器:

class Page extends MY_Controller {

    public $data = array();

    public function __construct() {
        parent::__construct();
        ...
        $this->load->library('widgetmanager');
    }

    public function index($slug = '') {

        echo $this->widgetmanager->show(2);
        echo $this->widgetmanager->show(1);

    }
}

我的图书馆

class WidgetManager
{

    private $CI;

    public function __construct()
    {
        $this->CI = & get_instance();
    }

    public function show($widget_id) {
        $data = array();
        $widget_id = (int)$widget_id;



        $this->CI->db->select('*');
        $this->CI->db->from('widget');
        $this->CI->db->where('id', $widget_id);

        $query = $this->CI->db->get();

        $item = $query->row_array();

        $data['widget_title'] = $item['title'];
        $data['widget_content'] = $item['content'];

        $widget =  $this->CI->load->view('widget/'.$item['source'], $data, TRUE);

        $data['widget_title'] = '';
        $data['widget_content'] = '';

        $this->CI->load->view('widget/'.$item['source'], $data);

        return $widget;
    }
}

小部件1:调用小部件/内容
窗口小部件2:调用窗口小部件/横幅

widget 1: Calls widget/content
widget 2: Calls widget/banner

正在发生的是,在第一个窗口小部件调用(它们与第二个窗口小部件调用的名称相同)上设置的变量被缓存,这意味着将第一个调用的值传递给同一调用.这很奇怪,因为存在不同的观点.

What is happening is, the vars set on the first widget call (they are same name as second widget call), get cached, meaning values from the first call are passed to same call. It is weird because are different views.

我尝试过:

  • 在库上执行load-> view之前和之后,使用clear_vars():$ this-> CI-> load-> clear_vars().
  • 使用空数组,null等调用load-> view
  • 试图在vars中添加带有窗口小部件slug的前缀(可以,但是我必须以某种方式将前缀发送到视图,因此不可能由于缓存问题)

有什么想法吗?

推荐答案

这是应该起作用的方法.

Here is what should work.

(我有简化您的数据库调用的自由度,使它需要更少的处理.)

(I took the liberty of simplifying your database call making it require much less processing.)

public function show($widget_id)
{
    $data = array();
    $widget_id = (int) $widget_id;

    $item = $this->CI->db
      ->get_where('widget', array('id' => $widget_id))
      ->row_array();

    $data['widget_title'] = $item['title'];
    $data['widget_content'] = $item['content'];

    $widget = $this->CI->load->view('widget/'.$item['source'], $data, TRUE);

    //clear the cached variables so the next call to 'show()' is clean
    $this->CI->load->clear_vars(); 

    return $widget;
}

进一步考虑调用$this->CI->load->clear_vars();可能毫无意义,因为每次调用WidgetManager::show()时,都会使用完全相同的键重新创建$data var.将$ data变量传递给load->view时,$ data ['widget_title']和$ data ['widget_content']的新值将使用这些键替换缓存的vars中的值.

On further consideration The call $this->CI->load->clear_vars(); is probably pointless because each time WidgetManager::show() is called the $data var is recreated with exactly the same keys. When the $data var is passed to load->view the new values of $data['widget_title'] and $data['widget_content'] will replace the values in the cached vars using those keys.

这篇关于在库中加载视图,缓存的vars问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆