Implementation of the PHP MVC pattern in the Web site architecture (1) _php tutorial

Source: Internet
Author: User
Tags php switch
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.

In XML, for example, it can be said that when XML is parsed, the DOM API has this kind of cognition? A node in a DOM tree knows where it is and what it contains. (When a node in an XML document is parsed with sax, it makes sense only if 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 don't make sense in the document, they mean just that PHP 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. The most common usage is the structure of a switch statement in index.php:

     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.

 
      db=mysql_pconnect ($host, $user, $pass);     mysql_select_db ($db, $this->db); }     //! An accessor/** * fetches a query resources and stores it in a local member * @param $sql string The database Query to run * @return void */function Fetch ($sql) {$this->query=mysql_unbuffered_query ($sql, $this->db) 
      
; Perform query here     }     //! An accessor     /**     * Returns an associative array of a query row     * @return Mixed     *     /function GetRow () { C7/>if ($row =mysql_fetch_array ($this->query,mysql_assoc))             return $row;         else             return false;}     ?

1

http://www.bkjia.com/PHPjc/446699.html www.bkjia.com true http://www.bkjia.com/PHPjc/446699.html techarticle The MVC pattern is very common in the site architecture. It allows us to create a three-tier application that separates the useful layers from the code, helping designers and developers work together and mention ...

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