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

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

问题描述

作为一个主要是自学的程序员,当我谈到设计模式等时,我已经迟到了游戏。我在使用CodeIgniter编写一个劳动管理webapp。

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的东西, say ...从数据库返回一行,约定是将它放在一个关联数组或标准对象中,其中的属性表示行中的数据。

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 类,并且数据将作为属性存储(例如 bedrooms bathroom address ),这些方法将代表你能用数据处理的有用的东西(例如 printInfo )可以打印($地址有$卧室卧室和$浴室浴室!))。

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!')).

我得到的印象 - 只是从我在互联网上看到的代码 - 这不是做事情的标准方式。你应该使用数组或通用对象,并说.. .do $ 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(表示抽象控制)架构。基本上,在PAC中,表示层由呈现者CodeIgniter调用控制器),在MVC中,View从模型中获取自己的数据。由于这种对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)和为什么人们看到MVCPHP代码谁来自于web开发之外的背景可以理解地变得混乱。 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天全站免登陆