Codeigniter缓存控制器问题



我有两个控制器:

cart/
cart/buy

在两者中显示库Cart 的内容

    <tbody>
        <?php foreach($this->cart->contents() as $items): ?>
        <tr>
            <td><?php echo $items['name'] ?></td>
            <td>$ <?php echo $this->cart->format_number($items['price']); ?></td>
            <td><?php echo $items['qty'] ?></td>
            <td>$ <?php echo $this->cart->format_number($items['subtotal']); ?></td>
        </tr>
            <?php endforeach; ?>
    </tbody>

我的问题是,当我将第一个项目添加到购物车时,控制器buy仍保留在缓存中。我的意思是,控制器cart/有5个项目,控制器cart/buy有1个项目。我必须按Ctrl+F5才能查看所有项目

我能够解决这一部分:

function buy()
    {
        $this->output->set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
        $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        $this->output->set_header('Pragma: no-cache');
        $this->output->set_header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');  
        if($this->cart->contents())
        {
            $this->load->view('web/products/buy_view');
        }
        else
        {
            redirect('cart');   
        }       
    }

但是,我想知道购物车中是否有数据,如果购物车是空的,我会重定向到另一个页面。

显然if($this->cart->contents()),"保留在缓存中",例如,cart可以在cart/中填充,但在cart/buy中为空并且直到我按下Ctrl F5条件仍然失败。

有没有办法解决这个问题,或者我做错了什么?


p.d.我在推车控制器中的添加方法:

function add_item()
    {
        if($this->cart_model->validate_add_item() == TRUE)
        {  
            redirect('cart');
        }
    }

我在购物车模型中的添加方法:

function validate_add_item()
    {
        $id = $this->input->post('producto_id'); 
        $cantidad = $this->input->post('cantidad');
        $this->db->select('vNombre, dPrecio');
        $this->db->where('iIdProducto', $id);
        $query = $this->db->get('product', 1);
        if($query->num_rows > 0)
        { 
            foreach ($query->result() as $row)  
            {  
                $data = array(  
                        'id'      => $id,  
                        'qty'=> $cantidad,  
                        'price'  => $row->dPrecio,  
                        'name'  => $row->vNombre  
                );  

                $this->cart->insert($data);   
                return TRUE;    
            }
        }
        else
        {  
            return FALSE;  
        }   
    }

我在您的代码中没有发现任何问题。我认为这是一个缓存问题,正如你提到的,所以你可以尝试清除你的缓存。这将清除代码点火器创建的所有以前的缓存,并防止将来创建缓存:

    $this->load->driver('cache');
    $this->cache->clean();
    $this->output->cache(0);

最新更新