MVC 应该如何在 CodeIgniter 中工作 [英] How is MVC supposed to work in CodeIgniter

查看:24
本文介绍了MVC 应该如何在 CodeIgniter 中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个主要是自学成才的程序员,在涉及设计模式等方面,我迟到了.我正在使用 CodeIgniter 编写劳动管理网络应用程序.

As a mostly self-taught programmer I'm late to the game when it comes to design patterns and such. I'm writing a labor management webapp using CodeIgniter.

我在学校做过一些 MVC ASP.NET/C# 和 Java,约定俗成是你的模型主要由代表实际对象的类组成,但也抽象了所有的数据库连接等等......这很漂亮我收集的标准 MVC 内容.

I did a bit of MVC ASP.NET/C# and Java at school, and the convention was that your model mainly consisted of classes that represented actual objects, but also abstracted all the database connection and such...which is pretty standard MVC stuff from what I've gathered.

我想我说的对,CI 完全抽象了数据库连接(除非你去看),这是课程的标准,你可以创建的模型可以抽象通用"CRUD 方法多一点创建一些对特定模型更有用的方法.

I imagine I'm right in saying that CI abstracts the database connection completely out of sight (unless you go looking), which is par for the course, and the models you can create can abstract the 'generic' CRUD methods a bit more to create some methods that are more useful for the specific model.

我有一个问题,因为它与我习惯的 MVC 不同,就是每当你说...从数据库中返回一行时,约定就是把它放在一个关联数组中或具有代表行中数据的属性的标准对象.

The thing I have a problem with, because it's different to what I'm used to with MVC, is that whenever you say...return a row from a database, the convention is to just put it in an associative array or standard object with the properties representing the data from the row.

在 ASP 中,您可以构建实际的类来存储此信息.例如,您有一个 House 类,数据将存储为属性(例如 bedroomsbathroomsaddress) 并且这些方法将代表您可以对数据执行的有用操作(例如 printInfo() 可能 print("$address has $bedrooms 卧室和 $bathrooms 浴室!')).

In ASP you'd have actual classes that you could construct to store this information. For example you'd have a House class and the data would be stored as properties (e.g. bedrooms, bathrooms, address) and the methods would represent useful things you could do with the data (e.g. printInfo() may print("$address has $bedrooms bedrooms and $bathrooms bathrooms!')).

我的印象是——仅从我在 Internet 上看到的代码来看——这不是做事的标准方式.你应该只使用数组或通用对象,然后说...做 $this->house_model->print_info($houseobject) 而不是 $houseobject->print_info();?

I'm getting the impression — just from code I've seen around the Internet — that this isn't the standard way of doing things. Are you supposed to just use arrays or generic objects, and say...do $this->house_model->print_info($houseobject) instead of $houseobject->print_info();?

谢谢.

推荐答案

尽管它声称相反,但 Codeigniter 根本不使用 MVC(事实上,很少有 Web 框架这样做!)它实际上使用了 PAC(Presentation-抽象控制)架构.本质上,在 PAC 中,表示层由演示者(CodeIgniter 称之为控制器)提供数据,而在 MVC 中,视图从模型中获取自己的数据.由于 MVC 的这种错误标记,存在混淆.

Despite its claims to the contrary, Codeigniter does not use MVC at all (In fact, very few web frameworks do!) it actually uses a PAC (Presentation-abstraction-control) architecture. Essentially, in PAC the presentation layer is fed data by a presenter (which CodeIgniter calls a controller) and in MVC the View gets its own data from the model. The confusion exists because of this mislabeling of MVC.

因此,CodeIgniter(以及大多数其他流行的 Web 框架)不鼓励使用适当的模型,而只是使用非常基本的数据访问其位置.由于胖控制器",这会导致大量问题.与域相关的代码都不可重用.

As such, CodeIgniter (and most of the other popular web frameworks) don't encourage a proper model but just use very rudimentary data access its place. This causes a large set of problems because of "fat controllers". None of the code which relates to the domain becomes reusable.

进一步阅读:

MVC 中的 M:为什么模型被误解和不被欣赏

模型-视图-混淆第 1 部分:视图从模型中获取自己的数据

这是 PHP 社区中普遍存在的混淆点(MVC 与 PAC 文章指出该问题源于 PHP 社区.该问题写于 2006 年,没有任何改变,如果有的话,情况变得更糟,因为有更多MVC 的错误定义的框架和教程.)以及为什么那些来自 Web 开发之外的背景的人看着MVC"PHP 代码变得困惑是可以理解的.PHP 中的大多数MVC"实现都不是 MVC.您认为模型应该正确构建是正确的,CodeIgniter 将自己标记为 MVC 是错误的.

This is a widespread point of confusion in the PHP Community (The MVC vs. PAC article identifies the problem as stemming from the PHP community. that was written in 2006 and nothing has changed, if anything it's got worse because there are more frameworks and tutorials teaching an erroneous definition of MVC.) and why people looking at "MVC" PHP code who have come from a background outside web development understandably become confused. Most "MVC" implementations in PHP are not MVC. You are correct in thinking models should be properly structured, it's CodeIgniter that's wrong to label itself as MVC.

这篇关于MVC 应该如何在 CodeIgniter 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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