codeigniter - 如果codeigniter模型返回0行,则显示自定义错误消息。如何扩展到一般情况?

标签 codeigniter error-handling models

我的模型中有多种功能可满足不同的目的。

例如,在URL字符串中查找给定$ _GET变量的数据。

我试图找出一种显示错误消息的方法,如果由于例如URL字符串操作导致数据库中没有匹配的行。

我的第一个想法是简单地在每次调用函数时返回错误消息(如果有错误),然后简单地使用if语句,如果有错误,则显示错误 View ,如果没有,则显示普通 View 。显示..

问题在于,此函数在我的 Controller 中被多次调用,而其他类似的函数在我的代码中被调用,需要类似的错误处理。

我不想在我的代码中数百万个类似的if/else语句来处理错误。

有人有更好的主意吗?
干杯

最佳答案

使用 session 类的flashdata项目。您可以串联错误消息并将其放入flashdata项目中以显示。

my_function(){
    // code that determines if there is an error returns => $error
    if ($error)
    {
        // concat previous and current errors
        $new_error = $this->session->flashdata('errors') . $error;
        // replace 'errors' with newly concatenated errors
        $this->session->set_flashdata('errors', $new_error);
    }
}

这将跟踪通过每个请求生成的所有错误,并允许您显示该特定请求的错误“列表”。
echo $this->session->flashdata('errors');

关于codeigniter - 如果codeigniter模型返回0行,则显示自定义错误消息。如何扩展到一般情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7430224/

相关文章:

c# - ASP.NET MVC - 使用 C# 将引用不同程序集的模型从 Controller 传递到 View

php - 如何通过codeigniter重定向带有数据的页面

php - 如何在 codeigniter 中使用两个不同的表只显示一次数据

vb.net - URL请求的返回值

python - 如何在 Django 中创建相关模型的实例

php - Codeigniter - 返回我的模型对象而不是 stdClass 对象

php - 使用foreach从数据库中获取数据到数组中

php - 笑脸助手代码点火器

python - 可执行文件失败时从subprocess.Popen()捕获错误

ruby-on-rails - React形式的服务器端Rails模型验证