CodeIgniter和Model-View-Controller - 你的经验/你的意思? [英] CodeIgniter and the Model-View-Controller – your experience / your meaning?

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

问题描述

我有一个关于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的超级元组分离。你所谓的控制器应该从用户的请求中收集数据,并将其传递给模型层的结构。模型层应该完全不知道前端。用于创建发票的域业务逻辑不会改变,只是因为您将发票编号的< input /> 从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和Model-View-Controller - 你的经验/你的意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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