梦想摆渡的专栏

原文地址上文已经提到。这里对其进行翻译和稍加说明。

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 iscode reusability and separation of concerns. In this section, we describe some general guidelines on how to better follow MVC when developing a Yii application.

模型 – 视图 -更好地

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;前端:一个向最终用户展示的公共web站点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 API:集成第三方应用接口或提供应用接口给第三方使用。

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

这些子应用可能为其他模块提供接口或者是使用其他yii应用的一些代码。

1. Model 模型

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

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, aSearchFormmodel, besides representing the search input data, may contain asearchmethod to implement the actual search.

例如,一个SearchForm模式,除了代表搜索输入数据,可能包含一个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, aNewsmodel may contain a method namedgetLatestNewswhich is only used by the front end; it may also contain a method namedgetDeletedNewswhich 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:

Define aNewsBasemodel class which only contains code shared by different sub-applications (e.g. front end, back end);

In each sub-application, define aNewsmodel by extending fromNewsBase. Place all of the code that is specific to the sub-application in thisNewsmodel.

News和这个模型相关的代码,放在News模型中。

So, if we were to employ this strategy in our above example, we would add aNewsmodel in the front end application that contains only thegetLatestNewsmethod, and we would add anotherNewsmodel in the back end application, which contains only thegetDeletedNewsmethod.

增加

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

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.

的请求的数据。请记住,一个模型可能是被用于多个完全不同的子应用(例如,单元测试,Web API)

旅行还在继续,这个过程是艰难而又孤单的。

梦想摆渡的专栏

相关文章:

你感兴趣的文章:

标签云: