A tentative approach to PHP implementation of MVC Classic Mode _php tutorial

Source: Internet
Author: User
MVC pattern PHP (as the current mainstream development language) implementation
  
Author: Harry fuecks translation: Easy Chen
The MVC pattern is very common in the site architecture. It allows us to create a three-tier application that separates useful layers from the code, helps designers and developers work together and improves our ability to maintain and extend existing programs.
  
   Views (view)
"View" mainly refers to the final result we send to the Web browser-for example, the HTML generated by our script. When it comes to views, many people think of templates, but it is doubtful that the template scheme is called the correctness of the view.
  
The most important thing for a view is that it should be "self aware", when the view is rendered (render), and the elements of the views are aware of their role in the larger frame.
  
With XML (standardization getting closer) as an example, it can be said that XML (standardization is getting closer) when parsed, the DOM API has the knowledge that a DOM tree node knows where it is and what it contains. (When an XML (normalization is getting closer) a node in a document is parsed with sax only if it makes sense when it resolves to that node. )
  
The vast majority of template scenarios use a simple process language and such template tags:
  
  

{Some_text}


  

{Some_more_text}


  
They have no meaning in the document, and they represent the meaning of PHP (which is now the mainstream development language) that will replace it with something else.
  
If you agree with this loosely described view, you will agree that the vast majority of template schemes do not effectively separate views and models. The template label will be replaced with what is stored in the model.
  
Ask yourself a few questions when you implement the view: "Is it easy to replace the whole view?" "" How long does it take to implement a new view? "Can you easily replace the description language of the view?" (such as replacing an HTML document with a SOAP document in the same view) "
  
   models (model)
The model represents the program logic. (often referred to as the business layer in an enterprise-level program)
  
In general, the task of the model is to convert the original data into data that contains some meaning that will be displayed by the view. Typically, the model encapsulates the data query, possibly through some abstract data classes (the data access layer). For example, if you want to calculate the annual rainfall in the UK (just to find a better place for yourself), the model will receive a daily rainfall of ten years, calculate the average, and pass it on to the view.
  
   controllers (Controller)
Simply put, the controller is part of the first call to the HTTP request that is entered in the Web application. It checks the received requests, such as some get variables, to make the appropriate feedback. Before you write out your first controller, it's hard to start writing other PHP code, which is now the mainstream development language. The most common usage is the structure of the switch statement in index.php (as the current mainstream development language):
  
   Switch ($_get[viewpage]) {
Case "News":
$page =new Newsrenderer;
Break
Case "Links":
$page =new Linksrenderer;
Break
Default
$page =new Homepagerenderer;
Break
}
$page->display ();
?>
  
This code mixes process-and object-oriented code, but this is usually the best choice for small sites. Although the above code can also be optimized.
  
A controller is actually a control that is used to trigger a binding between a model's data and a view element.
  
   Example
Here is a simple example of using the MVC pattern.
First we need a database access class, which is a generic class.
  
   /**
* A Simple class for querying MySQL (and PHP collocation of the best combination)
*/
Class DataAccess (Favorite of small website) {
/**
* Private
* $DB stores a database resource
*/
var $db;
/**
* Private
* $query stores a query resource
*/
var $query; Query Resource
  
//! A constructor.
/**
* Constucts a new dataaccess (favorite of Small website) object
* @param $host string hostname for DBServer
* @param $user string DBServer user
* @param $pass string dbserver user password
* @param $db String database name
*/
function DataAccess (favorite of small websites) ($host, $user, $pass, $db) {
$this->db=mysql (the best mix with PHP) _pconnect ($host, $user, $pass);
MySQL (and PHP collocation of the best combination) _select_db ($db, $this->db);

http://www.bkjia.com/PHPjc/509143.html www.bkjia.com true http://www.bkjia.com/PHPjc/509143.html techarticle MVC pattern PHP (as the current mainstream development language) implementation author: Harry Fuecks translation: The easy Chen MVC pattern is very common in the site architecture. It allows us to build a three-layer structure of ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.