我有 2 个 Controller :
cart/
cart/buy
在两者中显示库购物车的内容
<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>
我的问题是,当我将第一个商品添加到购物车时, Controller 购买仍保留在缓存中。我的意思是, Controller cart/ 有 5 件商品, Controller 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/,但是购物车/购买中为空,直到我按 Ctrl F5 条件仍然失败。
有什么办法可以解决这个问题,或者也许我做错了什么?
<小时/>p.d.我在购物车 Controller 中的添加方法:
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;
}
}
最佳答案
我没有发现你的代码有任何问题。我认为这是您提到的缓存问题,因此您可以尝试此方法来清除缓存。这将清除 codeigniter 之前创建的所有缓存,并防止将来创建缓存:
$this->load->driver('cache');
$this->cache->clean();
$this->output->cache(0);
关于Codeigniter缓存 Controller 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9879620/