标签: mvc 实践 | 发表时间:2014-04-16 01:50 | 作者:hy6533
原文地址 http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.best-practices

最佳MVC实践(Best MVC Practices)

Although Model-View-Controller (MVC) is known by nearly every Web developer, how to properly use MVC in real application development still eludes many people. The central idea behind MVC is code reusability and separation of concerns. In this section, we describe some general guidelines on how to better follow MVC when developing a Yii application.

虽然MVC已经被几乎所有的web开发者所熟知, 然而如何更好地应用到具体开发中仍然难倒了很多人. MVC的核心是代码重用和解耦. 围绕这个思想, 我们可以总结出一些关于更好遵循MVC去开发YII应用的基本方针.

To better explain these guidelines, we assume a Web application consists of several sub-applications, such as

为了更好的解释这些方针, 我们假设如下一个由多个子程序组成的web应用程序:

  • front end: a public-facing website for normal end users;
  • back end: a website that exposes administrative functionality for managing the application. This is usually restricted to administrative staff;
  • console: an application consisting of console commands to be run in a terminal window or as scheduled jobs to support the whole application;
  • Web API: providing interfaces to third parties for integrating with the application.

  • 前台: 一个可以由普通用户公开访问的站点.
  • 后台: 一个用于对你的web程序进行管理和函数化配置的站点, 一般仅限管理员访问.
  • 控制台: 一个由在终端窗口执行的控制台语句或支撑整个应用的调度任务组成的程序.
  • web api: 对第三方提供的接口.

The sub-applications may be implemented in terms of modules, or as a Yii application that shares some code with other sub-applications.

子程序可以用模块来实现, 或者可以作为一个可以喝其它子程序共享代码的YII应用.

1 Model
Models represent the underlying data structure of a Web application. Models are often shared among different sub-applications of a Web application. For example, a LoginForm model may be used by both the front end and the back end of an application; a News model may be used by the console commands, Web APIs, and the front/back end of an application. Therefore, models

1 模型

模型代表一个web应用的基础数据结构.模型通常被web应用中的不同子程序所共享.例如, 一个登陆表单模型可以被用在程序中的所有前端和后端; 一个新的模型可以被用在控制台命令, web apis, 以及程序的前端和后端,因此, 模型有以下特点:

  • should contain properties to represent specific data;
  • should contain business logic (e.g. validation rules) to ensure the represented data fulfills the design requirement;
  • may contain code for manipulating data. For example, a SearchForm model, besides representing the search input data, may contain a search method to implement the actual search.

  • 应该包含属性来代表特殊的数据
  • 应该包含业务逻辑来保证被代表的数据满足设计需要.
  • 应当包含数据操纵代码. 例如, 一个查询表单模型, 除了代表查询输入数据, 还需要包含一个方法来实现实际的查询动作.

Sometimes, following the last rule above may make a model very fat, containing too much code in a single class. It may also make the model hard to maintain if the code it contains serves different purposes. For example, a News model may contain a method named getLatestNews which is only used by the front end; it may also contain a method named getDeletedNews which is only used by the back end. This may be fine for an application of small to medium size. For large applications, the following strategy may be used to make models more maintainable:
一些时候, 根据上边最后一条规则, 让一个简单的类包含太多的代码, 会让模型变得很臃肿. 如果一个类包含很多目的,会变得很难维护. 例如, 一个新闻模型应该包含一个名叫获取最后一条新闻的方法, 这条方法仅被前台网站用到, 同时它还包括一个叫删除新闻的方法, 这个方法仅被后台使用对于一个小型或者中型应用来说, 上边的这种设计已经算不错了. 但对于一个大型应用, 下边的策略可以让你的model更易维护.

  • Define a NewsBase model class which only contains code shared by different sub-applications (e.g. front end, back end);
  • In each sub-application, define a News model by extending from NewsBase. Place all of the code that is specific to the sub-application in this News model.
  • 定义一个新新基类model, 它仅仅包含被前后台都使用的代码.
  • 在每一个子程序中, 定义一个新的model来继承基类model. 通过这种方法来实现子程序独有的特定功能/代码

So, if we were to employ this strategy in our above example, we would add a News model in the front end application that contains only the getLatestNews method, and we would add another News model in the back end application, which contains only the getDeletedNews method.

假如我们在之前的例子中使用上边的策略, 就应该在前台网站创建一个model, 它包含一个获取最后一条新闻的方法, 同时我们也需要在后台添加一个model, 包含删除新闻的方法.

In general, models should not contain logic that deals directly with end users. More specifically, models

一般来说, models不应该包含直接被最终用户执行的逻辑. 比如下边这些model的特例:

should not use $_GET, $_POST, or other similar variables that are directly tied to the end-user request. Remember that a model may be used by a totally different sub-application (e.g. unit test, Web API) that may not use these variables to represent user requests. These variables pertaining to the user request should be handled by the Controller.should avoid embedding HTML or other presentational code. Because presentational code varies according to end user requirements (e.g. front end and back end may show the detail of a news in completely different formats), it is better taken care of by views.

model不应该使用$_GET, $_POST, 或其他直接从最终用户请求中获取的类似变量.请记住模型应该服务于不同的子程序, 它们不需要用到这些变量来模拟用户. 用户变量由控制器来负责更为合适.

2 View 视图
Views are responsible for presenting models in the format that end users desire. In general, views


  • should mainly contain presentational code, such as HTML, and simple PHP code to traverse, format and render data;
  • should avoid containing code that performs explicit DB queries. Such code is better placed in models.
  • should avoid direct access to $_GET, $_POST, or other similar variables that represent the end user request. This is the controller's job. The view should be focused on the display and layout of the data provided to it by the controller and/or model, but not attempting to access request variables or the database directly.may access properties and methods of controllers and models directly. However, this should be done only for the purpose of presentation.

  • 应当主要由表现代码构成, 例如HTML, 其间穿插一些简单的PHP代码,格式和渲染数据.
  • 应该避免出现直接的数据库查询代码, 这类代码应当放在model中
  • 应当避免直接使用$_GET,$_POST 或者其它的来自用户的简单变量, 这应当是控制器的工作. 视图应该专注于显示控制器或模型提供的数据以及页面布局,且不要尝试直接使用request变量或数据库中的数据.可以直接访问控制器和model中的方法, 但应当是只读的.

Views can be reused in different ways:


  • Layout: common presentational areas (e.g. page header, footer) can be put in a layout view.
  • Partial views: use partial views (views that are not decorated by layouts) to reuse fragments of presentational code. For example, we use _form.php partial view to render the model input form that is used in both model creation and updating pages.
  • Widgets: if a lot of logic is needed to present a partial view, the partial view can be turned into a widget whose class file is the best place to contain this logic. For widgets that generate a lot of HTML markup, it is best to use view files specific to the widget to contain the markup.
  • Helper classes: in views we often need some code snippets to do tiny tasks such as formatting data or generating HTML tags. Rather than placing this code directly into the view files, a better approach is to place all of these code snippets in a view helper class. Then, just use the helper class in your view files. Yii provides an example of this approach. Yii has a powerful CHtml helper class that can produce commonly used HTML code. Helper classes may be put in an autoloadable directory so that they can be used without explicit class inclusion.
  • 布局: 通用的表现层(比如页眉,页尾等)可以放在布局中
  • 局部视图: 使用局部视图可以重用表现层的代码碎片.例如, 我们使用_form.php局部视图来渲染创建和更新页面中的模型输入表单.
  • 窗口小部件: 如果一些逻辑需要表现为局部视图, 这些局部视图应该被转化为小部件, 小部件的类文件是最好的存放逻辑代码的地方. 小部件所需的HTML标签可以放在小部件的视图文件中.
  • 帮助类: 在视图中我们有时候需要一些代码片段来完成一些简单的任务, 比如格式化数据或组织html标签. 相比把这些代码直接放在视图文件中, 更好的实现方式是把所有这些代码片段一个视图帮助类中. 我仅仅需要在视图中调用这个帮助类. YII提供一个这种实现的例子. YII有一个强大的CHtml帮助类, 它提供了一些通用的HTML操作代码. 帮助类应该放入一个自动加载目录, 这样他们就不需要在使用的时候指定加载.
3 Controller 控制器
Controllers are the glue that binds models, views and other components together into a runnable application. Controllers are responsible for dealing directly with end user requests. Therefore, controllers

控制器是把模型,视图和其它组件绑定到可运行程序的粘合剂.控制器负责直接处理用户端额请求. 因此 控制器

  • may access $_GET, $_POST and other PHP variables that represent user requests;
  • may create model instances and manage their life cycles. For example, in a typical model update action, the controller may first create the model instance; then populate the model with the user input from $_POST; after saving the model successfully, the controller may redirect the user browser to the model detail page. Note that the actual implementation of saving a model should be located in the model instead of the controller.
  • should avoid containing embedded SQL statements, which are better kept in models.
  • should avoid containing any HTML or any other presentational markup. This is better kept in views.

  • 可以访问$_GET,$_POST和其它来自用户请求的PHP变量
  • 可以创建model实例, 以及管理它们的生命周期. 例如, 在一个典型的模型更新动作中, 控制器会先创建一个模型实例, 然后从用户输入变量$_POST中获取数据进行填充, 最后成功保存这个模型, 最后, 控制器会让用户的浏览器跳转到模型详情页面.注意, 保存模型的实际代码位于模型实例而非控制器中.
  • 应当避免包含SQL语句, SQL应该写在model中.
  • 应该避免包含任何的HTML, 或者任何其它的视觉标记. 这些都应该放在视图中.

In a well-designed MVC application, controllers are often very thin, containing probably only a few dozen lines of code; while models are very fat, containing most of the code responsible for representing and manipulating the data. This is because the data structure and business logic represented by models is typically very specific to the particular application, and needs to be heavily customized to meet the specific application requirements; while controller logic often follows a similar pattern across applications and therefore may well be simplified by the underlying framework or the base classes.
在一个设计良好的MVC程序里, 控制器通常很苗条, 可能仅仅包含几十行代码; 然而model会变得很臃肿,包含很多数据操作代码. 这是因为model包含很多针对特定程序的数据结构和业务逻辑, 并且需要按复杂的用户需要呈现; 然而控制器逻辑通常位于程序访问之后, 可以作为框架和基类的基础.
作者:hy6533 发表于2014-4-15 17:50:35 原文链接
阅读:84 评论:1 查看评论

相关 [mvc 实践] 推荐:


- - CSDN博客架构设计推荐文章
原文地址 http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.best-practices 最佳MVC实践(Best MVC Practices). Although Model-View-Controller (MVC) is known by nearly every Web developer, how to properly use MVC in real application development still eludes many people.


- huige - 火丁笔记
Martin Fowler在他所写的《企业应用架构模式》一书中感慨道:MVC已经成为我们最常误用的模式. 人们之所以常常误用MVC,很大程度上是因为混淆了不同的MVC变体. 大概上世纪七十年代,Xerox PARC的Trygve提出了MVC的概念,并应用在Smalltalk系统中,为了和其它类型的MVC加以区分,历史上习惯的称之为Classic MVC.

Spring MVC 和 Struts2

- - CSDN博客架构设计推荐文章
Web层面的框架学习了三个Struts1和2,SpringMVC,那他们之间肯定存在一个优劣和适用的环境,Struts1和2的异同点我已经做过对比《 Struts1和Struts2》,这篇将对比下Struts2和SpringMVC的异同,下面数据基本来源于网络,本人是搜集整理所得,供大家参考. 一个项目使用什么样的技术,决定的因素很多,我所能想到的有:对系统的性能、开发的效率、团队学习的成本、业务场景等,下面尽量从这几个方面入手,来分析比较下他们之间存在的优劣.


- blankyao - YY in Limbo 混沌海狂想
拖到最后一天才开始准备,所以没时间写剧本照着念了T___T. 幻灯片上的吐槽跟实际的讲话其实是属于两个平行世界:. 新版阿尔法城背后的前端MVC实践 View more presentations from Dexter Yy.

srping mvc RequestMapping实现

- - CSDN博客推荐文章
spring mvc中定义请求的url只需要在方法上添加注解: @RequestMapping("aa.mvc")即可定义访问的url地址,但是你是否有考虑过为什么添加这个注解就可以实现url访问地址的定义了呢. 首先定义注解RequestMapping. mvc中常需要对输入值进行合法性校验,所以也定义了校验的注解MyValid.


- Dash - Becomin' Charles
由于采用了Web开发框架来开发项目,所以我首次在真正的项目中采用MVC的开发模式. 随着项目的不断深入,我也在不断反思,MVC设计模式到底给项目带来了什么. 听起来都很难听对吗,但是确实如此. 清晰的代码结构,易于维护,易于扩展. 当然,我不是在批判MVC,只是觉得,在使用MVC过程中,还是需要投入更深入的思考,到底怎样才能用好这个设计模式.


- - 博客园_首页
       在前面简述了从服务层到数据层参见 架构设计目录. 剩下了表现层,一个再好的中间层表现也必须有一个用户界面,提供和用户交互,将用户行为输入转化为系统操作,进入后台逻辑. 在当下RAD(快速应用开发)工具的支持下,我们可以比较快速的完成UI设计,RAD追求所见即所得的快速反馈,快速应用.


- - CSDN博客推荐文章
MVC是Model View Controller , 是模型-视图-控制器的缩写,一种软件设计典范.用于组织代码用 一种业务逻辑和数据显示分离的方法,这个方法的假设挑剔是如果业务逻辑被聚集到一个部件里面,而且界面和用户围绕数据的交互能被改进和个性化指定而不需要重新编写业务逻辑.. 三层架构是最基本的项目分层结果,而MVC则是三层架构的一个变体,MVC是一种好的开发模式.

Spring MVC 3 深入总结

- - 企业架构 - ITeye博客
大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了. Spring3 MVC结构简单,应了那句话简单就是美,而且他强大不失灵活,性能也很优秀. 官方的下载网址是: http://www.springsource.org/download   (本文使用是的Spring 3.0.5版本).

Spring MVC 与 web开发

- - 码蜂笔记
项目组用了 Spring MVC 进行开发,觉得对里面的使用方式不是很满意,就想,如果是我来搭建开发环境,我会怎么做. 下面就是我的想法,只关注于 MVC 的 View 层. 现在基本上都是用 ajax 来调用后台接口,拿到 json格式的数据再展示,有的人直接返回数据,却没有考虑异常的情况,我觉得返回的报文里必须包含表示可能的异常信息的数据和业务响应数据.