了解MVC [英] Understanding MVC

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

问题描述

最近几天,我在PHP中广泛阅读了有关OOP和MVC的书籍和网页,以便我能够成为一名更好的程序员。我在理解MVC时遇到一个小问题:



我在哪里放一个 mysql_query



我应该将其放在控制器中,并根据提供的查询调用返回数据的模型的方法吗?还是应该把它放在模型本身?是否提供全部垃圾的选项?

解决方案

关于MVC主题的材料



您可以列出您正在阅读的书籍,因为大多数(如果不是全部)php书籍,触及MVC是错误的。



如果你想成为一个更好的开发人员,我建议你从Marting Fowler的文章开始 -



我在哪里放SQL相关代码?



信息的存储和检索在数据源层处理,通常是以 DataMapper (不要混淆使用该名称的ORM)。



以下是它的简化使用如下:

  $ mapper = $ this-> mapperFactory->建立(Model\Mappers\User ::类); 
$ user = $ this-> entityFactory-> build(Model\Entities\User :: class);

$ user-> setId(42);
$ mapper-> fetch($ user);

if($ user-> isBanned()&&&user> hasBannExpired()){
$ user-> setStatus(Model\Mappers\User :: STATUS_ACTIVE);
}

$ mapper-> store($ user);

如你所见,域对象甚至不知道它的信息是存储的。而且您也不会将数据放在哪里。它可以存储在MySQL或PostgreSQL或一些noSQL数据库中。或者也许推送到远程REST API。或者也许是映射器是测试的模拟器。所有您需要做的更换映射器,都是以不同的工厂提供此方法。



另请参阅这些相关帖子:




The last few days, I have extensively read books and web pages about OOP and MVC in PHP, so that I can become a better programmer. I've come upon a little problem in my understanding of MVC:

Where do I put a mysql_query?

Should I put it in the controller and call a method on a model that returns data based on the provided query? Or should I put it in the model itself? Are both of the options I'm providing total garbage?

解决方案

Materials on the subject of MVC

You could have listed the books you were reading, because most (if not all) php books, which touch on MVC, are wrong.

If you want to become a better developer, i would recommend for you to start with article by Marting Fowler - GUI Architectures. Followed by book from same author - "Patterns of Enterprise Application Architecture". Then the next step would be for you to research SOLID principles and understand how to write code which follows Law of Demeter. This should cover the basics =]

Can I use MVC with PHP ?

Not really. At least not the classical MVC as it was defined for Smalltalk.

Instead in PHP you have 4 other patterns which aim for the same goal: MVC Model2, MVP, MVVM and HMVC. Again, I am too lazy to write about differences one more time, so I'll just link to an old comment of mine.

What is Model ?

First thing you must understand is that Model in MVC is not a class or an object. It is a layer which contains multitude of classes. Basically model layer is all of the layers combined (though, the second layer there should be called "Domain Object Layer", because it contains "Domain Model Objects"). If you care to read quick summary on what is contained in each part of Model layer, you can try reading this old comment (skip to "side note" section).

                            
The image is taken from Service Layer article on Fowler's site.

What does the Controllers do ?

Controller has one major responsibilities in MVC (I'm gonna talk about Model2 implementation here):

Execute commands on structures from model layer (services or domain objects), which change the state of said structures.

It usually have a secondary responsibility: to bind (or otherwise pass) structures from Model layer to the View, but it becomes a questionable practice, if you follow SRP

Where do I put SQL related code ?

The storage and retrieval of information is handled at the Data Source Layer, and is usually implemented as DataMapper (do not confuse with ORMs, which abuse that name).

Here is how a simplified use of it would look like:

$mapper = $this->mapperFactory->build(Model\Mappers\User::class);
$user = $this->entityFactory->build(Model\Entities\User::class);

$user->setId(42);
$mapper->fetch($user);

if ($user->isBanned() && $user->hasBannExpired()){
    $user->setStatus(Model\Mappers\User::STATUS_ACTIVE);
}

$mapper->store($user);

As you see, at no point the Domain Object is even aware, that the information from it was stored. And neither it cases about where you put the data. It could be stored in MySQL or PostgreSQL or some noSQL database. Or maybe pushed to remote REST API. Or maybe the mapper was a mock for testing. All you would need to do, to replace the mapper, is provide this method with different factory.

Also, please see these related posts:

这篇关于了解MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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