yiiapi/docs/guide/zh_cn/basics.best-practices.txt

(开头部分) 7KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

MVC最佳实践
==================

虽然几乎每一位Web开发者都了解模型-视图-控制器(MVC),但在一个真实应用的开发中,如何正确使用MVC仍然不被很多人重视。MVC的中心思想是**代码复用和解耦**。在这一节中,我们将介绍一些通用原则,以便在Yii应用中更好地实现MVC。

为了更好地阐述这些原则,我们假定有一个Web应用,其中包含以下几个子应用:

* 前端:一个面向最终用户的公开网站;
* 后端:一个实现管理功能的应用。这通常仅限管理组使用;
* 终端:一个运行于终端窗口或用以支撑整个应用的计划任务的终端命令应用。
* Web API:提供一系列Web接口用以整合第三方应用。

子应用可能以[模块](basics.module)的形式实现,或者以一个Yii应用的形式实现并且和其他子应用共享某些代码。


模型
-----

[模型](basics.model) 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

* 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:

* 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.

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.

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.

* 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.


View
----

[Views](/doc/guide/basics.view) 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.


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.
后面还有 24 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。