PS:这本书感觉不怎么样,这么多低频词就倒人胃口。。。

Suppose you'v recently launched a new web site, only to find that it's soon inundated(淹没) with users. Eager to extend this new found success, the project begins to grow in ambition and complexity. You'v even begun to hire a few talented staff members to help out with the design and development. The newly hired designers immediately begin an overhaul(检修) of the site's pages, many of which currently look like this:

<?php

// Include site configuration details and page header
INCLUDE "config.inc.php";
INCLUDE "header.inc.php"; // Scrub some data
$eid = htmlentities($_POST['eid']); // Retrieve desired employee's contact information
$query = "SELECT last_name, email, tel
FROM employees
WHERE employee_id = '$eid'";
$result = $mysqli->query($query, MYSQLI_STORE_RESULT); // Convert result row into variables
list($name, $email, $telephone) = $result->fetch_row();
?> <div id = "header">Contact Information for: <?php echo $name; ?>
Employ Name: <?php echo $name; ?> <br />
Email: <?php echo $email; ?> <br />
Telephone: <?php echo $telephone; ?> <br /> <div id="sectionheader">Recent Absences <?php
// Retrieve employee absences in order according to descending date
$query = "SELECT absence_date, reason
FROM absences WHERE employee_ids='$eid'
ORDER BY absence_date DESC ";
// Parse and execute the query
$result = $mysqli->query($query, MYSQLI_STORE_RESULT); // Output retrieved absence information
while (list($data, $reason) = $result->fetch_row()) echo "$date: $reason"; // Include page footer
INCLUDE "footer.inc.php";
?>

Becaus the design and logic are inextricably intertwined(纠缠在一起), several problems soon arise:

  • Because of the intermingling(混合、掺和) of the site's design and logic, the designers who were hired with the sole purpose of making your web site look great are now faced with the task of having to learn PHP.
  • The developers, who were hired to help out with the expansion of web site features, are distracted by fixing the bugs and security problems introduced by the disigner's novice(新手) PHP code. In the process, they decide to make their own little tweaks to the site design, infuriating(惹怒) the designers.
  • The almost constant conflicts that arise due to simultaneous editing of the same set of files soon become tiresome and time consuming.

You're probably noticing a pattern here: the lack of separation of concerns is breeding an environment of pain,  distrust, and inefficiency. But there is a solution that can go a long way toward alleviating(缓解) these issues: the MVC architecture.

The MVC approach renders development more efficient by breaking the application into three distinct components:the model, the view, and the controller. Doing so allows for each component to be created and maintained in isolation, thereby minimizing the residual(剩余) effects otherwise incurred should the components be intertwined in a manner similar to that illustrated in the previous example. You can find detailed definitions of each component in other learing resources, but for the purposes of this introduction the following will suffice(suffic-ient):

  • The model: The model specifies the rules for the domain modeled by your wesite, defining both the application's data and its behavior. For instance, suppose you create an application that serves as a conversion calculator,  allowing users to convert from pounds to kilograms, feet to miles, and Fahrenheit to Celsius, among other units. The model is responsible for defining the formulas used to perform such conversions, and when presented with a value and desired conversion scenario(无法理解,就当比率吧), the model carries out the conversion and returns the result. Note that the model is not responsible for formatting the data or presenting it to the user. This is handled by the view.
  • The view: The view is responsible for formatting the data returned by the model and presenting it to the user. it's possible for more than one view to utilize the same model, depending on how the conversion application: one targeting standard browsers, and one aptimized for mobile devices.
  • The Controller: The controller is responsible for determing how the application should respond based on events occurring within the model and the view to produce the appropriate response. A special controller known as a front controller is responsible for routing all requests to the appropriate controller and returning the response.

To help you better understand the dynamics of an MVC-driven framework, the following example works through a typical scenario involving the converter application, highlighting the role of each MVC component:

1. The user interacts with the view to specify which type of conversion he'd like to carry out, for instance converting an input temperature from Fahrenheit to Celsius.

2. The controller responds by identifying the appropriate conversion action,  gathering the input, and supplying it to the model.

3. The model converts the value from Fahrenheit to Celsius and returns the result to the controller.

4. The controller calls the appropriate view, passing along the calculated value. The view renders and returns the result to the user.

最新文章

  1. partial class的使用范围
  2. UI,切图,命名
  3. 本地连接虚拟机上面的redis
  4. angularjs $q、$http 处理多个异步请求
  5. PHP学习(一)----变量及字符串
  6. iOS 开发 证书总结 开发证书和生产证书的区别
  7. 手势识别官方教程(7)识别缩放手势用ScaleGestureDetector.GestureDetector和ScaleGestureDetector.SimpleOnScaleGestureListener
  8. Codec plugins ? multiline
  9. nginx 配置文件解析(一)
  10. tomcat web项目部署方式
  11. Hibernate 笔记1
  12. 硬盘重装Ubuntu12.04的感受
  13. Linux/UNIX先进I/O
  14. 4、Spring+MyBatis增删改查
  15. 使用python scipy.optimize linprog和lingo线性规划求解最大值,最小值(运筹学学习笔记)
  16. sparkSQL catalyst
  17. 双击jar文件运行程序
  18. 第四周学习总结-HTML
  19. Oracle(字符函数)
  20. Flask源码阅读-第二篇(flask\__init__.py)

热门文章

  1. 理解Python的迭代器(转)
  2. request (请求对象)
  3. CCF 第六次计算机职业认证 第四题 收货 stl动态存储和fleury算法的综合应用
  4. [2018湖南省队集训] 6.28 T2 color
  5. [UOJ62]怎样跑得更快
  6. 通过OpenVPN来穿越上网(未实践)
  7. svn安装和配置
  8. 你家的Wifi密码安全吗?
  9. 【hibernate】错误:org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was altered from xx to xx
  10. 【js】js截取字符串 +转化字符串为数字