CodeIgniter 和模型-视图-控制器——你的经验/你的意思? [英] CodeIgniter and the Model-View-Controller – your experience / your meaning?

查看:24
本文介绍了CodeIgniter 和模型-视图-控制器——你的经验/你的意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 CodeIgniter MVC 原理的简单"问题.如果我查看 CI(模型)的手册,我会看到例如:

I have a "simple" question about the principle from the CodeIgniter MVC. If I take a look in the manual from CI (Models) I see for example this:

function insert_entry()
{
    $this->title   = $_POST['title']; // please read the below note
    $this->content = $_POST['content'];
    $this->date    = time();
    $this->db->insert('entries', $this);
}

好吧——我知道以这种方式输入数据是不好的 :) 但如果我们使用$this->input->post()"……对我来说,它看起来并不好.在我使用模型中的函数之前处理控制器中的数据不是更好吗?也许模型部分看起来是这样:

Well, ok – to put in data this way is bad I know :) but also if we user "$this->input->post()" … for me it doesn’t look better. Isn’t it better to handle the data in the controller before I use a function from a model? Maybe the model part looks so:

function insert_entry($data)
{
    $this->db->insert('entries', $data);
}

在这样的控制器中:

$this->load->model('Blog');
$data = array();
$data['title'] = $this->input->post('title');
$data['content'] = $this->input->post('content');
$this->Blog->insert_entry($data);

但是我在哪里运行验证等......模型还是控制器?也许有人明白我想知道什么.也许你有更多的经验,链接或其他什么.谢谢!

But where i run the validation etc. … model or controller? Maybe someone understand what I would like to know. Maybe you have some more experience, links or whatever. Thanks!

推荐答案

如果您正在尝试使用 CodeIgniter 实现正确的 MVC 或 MVC 启发的设计模式,那么您已经失败了.CodeIgniter 没有遵循 MVC 和相关模式的思想.它实际上只是克隆了 Rails 中使用的模式(我可以在评论部分详细说明,如果你想知道为什么以及如何).

If you are trying to implement proper MVC or MVC-inspired design pattern with CodeIgniter, you have already failed. CodeIgniter does not follow the ideas of MVC and related patterns. It actually just clones the pattern used in Rails (I can elaborate in comments section, if you want to know why and how).

也就是说……

在控制器中使用 $this->input->post() 的原因是提供一些抽象并将您的代码与 PHP 的超全局变量分开.你所说的控制器"应该从用户的请求中收集数据并将其传递给模型层的结构.模型层应该完全不知道前端.创建发票的域业务逻辑不会仅仅因为您将发票编号的 从innr"重命名为number"而改变.

The reason why $this->input->post() is used in controllers is to provide some abstraction and separate your code from PHP's superglobals. What you call a "controller" should collect data from the user's request and pass it to the model layer’s structures. The model layer should be completely unaware of the front-end. The domain business logic for creating an invoice does not change just because you renamed the <input/> for invoice number from "innr" to "number".

数据验证应该发生在模型层.正确完成后,验证代码是域对象的一部分,并且将处理数据完整性检查通过存储抽象(例如,数据映射器),但在 CodeIgniter 中,人们通常将这两个域混为一谈和存储逻辑一起并称之为:模型".当然,这违反了SRP,但 CI 用户并不关心,甚至不知道这些原则.所以基本上,在为 CI 编写时,验证应该在模型"中进行.

The data validation should happen in the model layer. When done properly, the code for validation is part of domain objects and data integrity checks would be handled by storage abstraction (for example, a data mapper), but in CodeIgniter people usually lump both domain and storage logic together and call it: "models". Of course that violated SRP, but CI users don't care and are even unaware of such principles. So basically, when writing for CI, the validation should happen in "models".

如果您想阅读有关整个主题的更多信息,您可能会发现这篇文章相关.

If you want to read more about the whole subject, you might find this post relevant.

这篇关于CodeIgniter 和模型-视图-控制器——你的经验/你的意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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