如何在CodeIgniter中显示错误消息 [英] How to display error messages in CodeIgniter

查看:117
本文介绍了如何在CodeIgniter中显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器中,我将此代码检查购物车是否为空:

In my controller I put this code to check if the shopping cart is empty:

if (!$this->cart->contents()){
    $this->session->set_flashdata('message', 'Your cart is empty!');
}

$this->data['message'] = $this->session->flashdata('message');

$this->load->view('templates/header', $this->data);
$this->load->view('bookings', $this->data);
$this->load->view('templates/footer');

在我的预订视图中,我设置此代码以显示消息:

On my "bookings" view I set this code to display the message:

<div id="infoMessage"><?php echo $message;?></div>

但是在第一页加载消息您的购物车是空的!不显示。

But on the first page load the message "Your cart is empty!" is not showing. However, it will display when I press the F5 key or refresh the browser.

我已经读过codeigniter的说明书,说CodeIgniter支持flashdata,或会话

I have already read the manual from codeigniter that says "CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared."

但是,我不知道在哪里设置此消息,因此它将被

However, I don't know where to set this message so it will be available on "next server requrest".

推荐答案

不需要将数据传递给视图并将其分配为flashdata。

There is no need to pass the data to the view and assign it as flashdata.

它第一次无法工作的原因是,flashdata仅适用于下一个服务器请求。加载视图不是服务器请求。但是,刷新页面是

The reason that it's not working the first time, is that flashdata is only available for the next server request. Loading the views isn't a server request. However, refreshing the page is.

有以下几种解决方案:


  • 直接传递数据并加载视图。 (不使用Flash数据。)

  • 使用flashdata并重定向到页面。

你可以直接传递它(个人而言我会使用这个选项 - 我不知道为什么要在这种情况下使用flashdata ,我想你总是想通知用户他们的购物车是否为空...):

You could just pass it directly (personally I'd go with this option - I'm not sure why you'd want to use flashdata in this case, I would've thought that you'd always like to inform the user if their cart is empty...):

Controller:

if (!$this->cart->contents())
{
    $this->data['message'] = 'Your cart is empty!';
}

$this->load->view('templates/header', $this->data);
$this->load->view('bookings', $this->data);
$this->load->view('templates/footer');

查看

<div id="infoMessage"><?php echo $message;?></div>



使用flashdata



要使用flashdata,则:

Using flashdata

Or, if you want to use flashdata, then:

控制器

if (!$this->cart->contents())
{
    $this->session->set_flashdata('message', 'Your cart is empty!');
}

redirect('/your_page');

查看

 <div id="infoMessage"><?php echo $this->session->flashdata('message');?></div>

如果你想使用这种方法,你不能简单地加载视图 - 服务器请求问题 - 您必须重定向用户,使用 redirect('/ your_page'); 网址助手 $ this-> load-> helper('url'); 。您还可以使用本机PHP 标头发送适当的HTTP标头, 功能。第一次加载视图将不起作用。

If you want to use this method, you can't simply just load the view - due to the server request issue that I explained above - you have to redirect the user, using redirect('/your_page');, and the URL Helper $this->load->helper('url');. You can also send the appropriate HTTP header using the native PHP header function. Loading the view the first time won't work.

您可以使用 $ this- > session-> keep_flashdata('message'); 以保存其他请求的数据。

You can use this $this->session->keep_flashdata('message'); to preserve the data for an additional request.

您可以自动加载会话库或将其加载到控制器的构造函数中: $ this-> load-> library('session');

I'd also check that your either auto-loading the session library or loading it in the constructor of your controller: $this->load->library('session'); (I'm fairly sure you will be, but it's worth checking anyway.)

最后,我知道有些人在数据库中存储会话数据时可能会遇到flashdata的问题,因此,

Finally, I know that some people who store session data in their database can have issues with flashdata, so that may be worth looking into, if none of the above helps.

在旁注中,我个人会有一个检查,以确保变量在回声之前设置

On a side note, personally I'd have a check to ensure that a variable is set before echoing it.

这篇关于如何在CodeIgniter中显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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