Introduction to using MVC in PHP (1)

Source: Internet
Author: User

(Walter. Fan compiled an introduction to MVC using PHP written by Jason E. Sweat)

Today, MVC is widely used in B/S, and I am afraid it is the most famous design pattern.
For website, it is a good architecture.
The so-called MVC Model-View-controller is an object-oriented design model. MVC uses classes to place business logic (where the data is stored, which users are allowed to process the data, and how the data is processed) in the model, put the representation logic (how the data from the model is expressed) in the view, and put the entire process control of the application in the controller.

PS. The design pattern is the programmer's internal power method, the 9 Yin Zhenjing of the relevant design patterns is the "design patterns" of the "Four help". On the http://www.phppattenrs.com, there is an introduction to the application of the Design Pattern in PHP.

Why use MVC?
MVC is an excellent solution for website problems. in the website, clicking on each page is the interaction between the user and the system (input ). suppose you have some requirements to maintain the state between the stored data and the display mode. The MVC mode is a very suitable architecture for your application.

The MVC mode focuses on three types of problems when establishing a solution
1. Process persistent data (databases or files)
2. process the logical control flow of the application, display to the user, and allow the user to perform actions on the data in the Application
3. display information to users in the Application

Comparison between JSP and J2EE in MVC applications
Http://java.sun.com/blueprints/patterns/MVC.html
Http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/webtier/web-tier5.html
Http://developer.java.sun.com/developer/onlineTraining/JSPIntro/contents.html#JSPIntro4

"Model 1" and "Model 2" mentioned in JSP specifications"
Model 1: The model is encapsulated in a class in the system, but the view is scattered in different la s, and there is no page flow controller.
Model 2: recommendation method. The controller object is responsible for application process control.

JSP/J2EE world has a lot of very exquisite MVC implementation scheme, which has the name of struts (http://jakarta.apache.org/struts)

Model
Models is a component that implements business logic in MVC applications.
Business logic is any PHP logic Code related to information storage
The term model comes from the real world. It is similar to rules and data.
A model is the only component in the system that deals with persistent data storage.
(For example, saving or retrieving data in a Web request ).
The model can implement any technology in Web development: Database Calling, file processing, session management, web services, or other means of storing data.

A well-designed model can be used by other applications on the Web server.
When you start to understand the power of encapsulating business logic with these classes, you will clearly realize that creating a good model is a very useful habit, even if you do not need a full MVC Architecture

For models that interact with database tables, it is best to have an associated class for a single table.
This will help us get the First Advantage of MVC: processing data in a certain persistent data storage
As long as we stick to the MVC framework, if the database table is changed, we only need to check the corresponding model in the system.
Typical examples of web applications are users, visitors, shopping carts, directory items, orders, titles, or articles.
In database-driven Web applications, the model tends to be associated with a single data table, or closely coupled tables (or views ). as a reasonable naming rule, the name of a model class can be similar to that of a database table.
I also tend to write model classes for website functions. For example, a multi-page form can have an associated model wizard class.

View
A view is a component that represents output to users in an MVC application.
The most common PHP application output is HTML, but the view is not limited to this.
The MVC application can also output XML, WML, plain text, images, emails, or other content.
PHP has a wide range of applications in the view. It sends data to the browser and implements template solutions.
In general, the view creates an instance of the model and uses the model method to obtain and present the data to the user.
In the application, the view contains all the PHP code required to convert binary data into the required data representation format. For example, the Code drawn using the GD library belongs to the view in MVC.

Controller
The Controller is the core component of the MVC application. It must know the user's HTTP Request (or command line input parameters) to decide what view to display or what other actions to generate the application.
In short, controllers are the core component of any MVC Project (mainly because models and views must be applied to your applications based on their nature ). different projects have different policies to determine how views and models interact.
The interaction between these objects is system coupling. if a configuration file (usually XML-based files) is used to define the relationship between them, this is called "loose coupling ". the opposite approach is to hard-code the application process in the system.

Describes the application of various web development technologies in the three MVC components.

In practice, these technologies may overlap. For example, if your application contains a user model,
You may want to use the user model to send a cookie value to the client browser. we should assign this task to the Controller through HTTP. in addition, we may want to consider where to display the instance representation view of a model and which view to display through URL parameters. as a result, the view may access this information through $ _ get. (connect the Controller to the view through HTTP ). we should not see the cross-use of these technologies between views and models.
HTML should not be included in the model, and any database access function should never be included in the view.
Any of the above practices violates the principle of separating content from format, breaks the MVC pattern, and limits the flexibility of applications.

There are already many PHP-based Open Source Projects on the Internet
Ambivalence
Http://amb.sourceforge.net/
This project is a port of the Java project called

Maverick (http://mav.sourceforge.net/), which originated
As an attempt to simplify struts.

Eocene
Http://eocene.net/
An MVC framework with a goal to port to ASP. NET.

PHP. MVC
Http://www.phpmvc.net/
A port of the Java-based Jakarta Struts Framework

Phppatterns () MVC2
Http://www.phppatterns.com/index.php/article/articleview/19/1/1/
The second article on this site about the MVC pattern.

Phrame
Http://phrame.itsd.ttu.edu/
Another port of the Struts framework.

Combined with project practices
Generally, PHP-based MVC applications have two general process paths.
The first process path is to send a request to obtain a specific view.
The second process path is the data that the user inputs and updates the model.

First process path
1. The Controller is responsible for interpreting the requests sent through http get.
The request determines which view to switch.
2. the application process is transferred to the view. Generally, the view needs to display the response result from the model data.
3. view request to obtain data from the model.
The model is the only component that can access persistent data storage containers (databases or files.
4. The model queries data from the persistent data storage container.
5. The model obtains data from the persistent data storage container.
6. The model returns data to the view.
If the view needs data from the current or other models, repeat steps 3 to 6 as needed.
7. format the view and send the output to the user as a response.

Second process path
Another common process of MVC Web applications is to update data by users in applications.
Describe the process path
1. The controller receives the request (similar to submitting the request in http post mode from the user's browser)
2. The Controller determines what action to take and calls the method of the model object with appropriate parameters to respond to this request.
The model determines whether the request is valid based on the business logic in the code.
3. If the request is valid, the model calls the corresponding method to change the data.
4. Return the update result.
5. The model returns the control to the Controller.
6. The Controller determines the next point of the user.
7. Redirect to the corresponding address. After the header () command is published, this request ends.

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.