将数据传递到编码器点火器中的控制器后,如何使用新 URL 调用视图


How do i call view with fresh URL after passing data to controller in codeigniter?

我用下面的网址来调用我的产品视图:

example.com/diamondshop/index.php/product/view

产品控制器中的视图功能示例:

function view($msg = "") {
    $data['msg'] = $msg;
    $this->load->view('product', $data);
}

通过单击"添加到购物车"按钮,我调用控制器,如下所示 url:

example.com/diamondshop/index.php/product/addcart/3

在这里,3 是我的产品 ID。

产品控制器中的加卡功能示例:

function addcart($pid) {
    //
    // My code to add in cart
    //
    $this->view("Item Added to Cart.");
}

我现在再次进入产品视图,但在加载视图后,我的 URL 保持原样:

example.com/diamondshop/index.php/product/addcart/3

但是,当我第一次调用产品视图时,我想要像上面这样的新 URL:

example.com/diamondshop/index.php/product/view

那么,如何使用新 URL 调用产品视图?

使用重定向功能:

redirect('/product/view/', 'refresh');

$this->view("Item Added to Cart.");后添加此内容

以下是更多 URL 帮助程序:http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

问题是 CodeIgniter 只是在同一请求(即从 addcartview)中从一个函数跳到另一个函数,这不会改变您的 URL。如果您希望更改 URL,则需要在 addcart 函数的末尾执行标头重定向。这可以使用 URL 帮助程序完成,它是redirect方法。

redirect('/product/view');

关于第二个参数,以下是手册所说的:

可选的第二个参数允许您在"位置"方法(默认)或"刷新"方法之间进行选择。位置更快,但在Windows服务器上有时可能会有问题。