An Analysis of MVC programming and mvc programming in PHP programming

Source: Internet
Author: User

An Analysis of MVC programming and mvc programming in PHP programming

The MVC programming idea of PHP has been widely used in the development of various large projects. Many mature MVC frameworks are gradually known and widely used in various projects, common examples include ThinkPHP, codeigniter, Symfony, yii, and cakePHP. This article briefly introduces the MVC programming idea of php.

1. What is MVC?

Simply put, the website source code is classified and layered.
The meanings of the three MVC letters:
M: Model, responsible for database operations.
V: View, which calls the Model to retrieve data and then calls the template to display the final effect.
C: The Controller, the program entry, decides which View to call and tells the View what to do.
In this case, the execution order of the program is C-V-M or C-M, And the MVC name is exactly the opposite.

Ii. Why MVC?

1. Make the physical structure of website programs more reasonable.

When using PHP to build a website, you may build a PHP file for each page in the most stupid way. If your website only has index. php, menu. php. article. three php pages, you don't need MVC, but when we do a General website, we usually have dozens of pages, and it is obviously not acceptable to put all the pages in the root directory, so you need a reasonable idea to classify your code, divide them into different directories by function, and load and call the code intelligently. This is what MVC will help you do.

2. Make the code easier to maintain.

Let's take a look at a single page. The most stupid way is to mix PHP code with HTML code. This is obviously not good enough. You have to distinguish between PHP and HTML when maintaining your website, this is a disaster for a programmer. As a result, many people use Smarty to separate "data processing" from "page display". This is indeed good, and many people are doing this, but it is not MVC, MVC divides data processing into logical processing and database operations.
In this way, when your program is wrong or you want to modify it, it becomes easy. When an error is displayed on the page, you can check the V or template file; when there is a logic problem, you should check C and V; when your database operation is wrong, you should check M.
In fact, MVC generally divides a PHP page into four pages: C, V, M, and template. Perform their respective duties to facilitate management.

3. Facilitates code reuse.

MVC usually puts a large function under a directory, that is, it is managed by a C.
For example, to create a website with a membership system, we can put all the Code related to Members in the user directory, which is managed by User_Controller in a unified manner. When another website also needs a membership system, we can directly copy the Directory and modify the interface.

Iii. idea of implementing MVC in PHP

We need three base classes: Controller, View, and Model. Different C, V, and M inherit from each other and they have corresponding attributes and methods. If you do not understand them here, you can read the object-oriented book.

Here we provide you with a design idea for the MVC base class for your reference only:

1.Controller class design

A main () method is called by the program. The get and post variables are used to determine how to handle the problem.
A getModel ($ model) method calls the M of the corresponding directory when you need to call the database.
A display ($ view) method, called in the main () method, loads the corresponding V, and drops the main () method of V;

2.The design of the View class is similar to that of the Controller.

A main () method. This method is called when C loads V so that the program can continue to run.
A getModel ($ model) method calls the M of the corresponding directory when you need to call the database.
A display ($ template) that calls the corresponding template file and passes the data to the template.

3.Model class design

You can define some attributes, such as tables to be operated and fields to be operated.
A getDB () method to obtain an instance of a Database Class (database classes are generally designed in single-piece Mode)
Load () method to load a data.
An add () method automatically constructs an SQL statement based on the defined attributes and performs the insert operation.
An eidt () method, same as above, but is modified.
One del () method, the same as above, but the delete operation is performed.
To help new users better understand the working principles of my thinking, we now simulate a user login scenario to see how MVC works.
Now assume that all data is submitted to index. php,

Step 1:
We submit various get variables to tell index. php which C should be used, for example, index. php? Controller = user
Then index receives the get variable and does not need to do anything. You can directly find/user/controller. php, throwing all the data to him. Originally, GET and POST are global, so index. php does not need to do anything. You can directly call the main function of C. php task completed.

Step 2:
C's main function starts execution, checks the variables, and finds the login operation to be performed by the user (very simple, you can post the variable do = login), so call getModel, load the corresponding M class (for example,/user/models/model. php), and instantiate, call the load method of the instance, load the user's data, determine whether the password is consistent with the user submitted, if the submitted data is incorrect header jump to the error page, if it is correct, call the display () method to load the corresponding V (for example,/user/views/details. php), and instantiate it, call its main () function, and enter the third step. The task C has been completed, and the second operation is not performed in the main function.

Step 3:
You can choose to call getModel () to load M, rewrite and retrieve data, or pass the parameter (for example, SESSION) when C instantiates V. After V has determined the data, display (), load the template, and MVC execution is complete.
Of course, due to the limited words and energy, the content here is a very brief summary. Many details should be taken into account during actual implementation. However, when designing MVC, the general idea is like this, in practice, I feel good.


What are the important website programming ideas of php?

First, you need to understand what MVC is used. MVC is used for cooperative development. For example, you have a development team. You are the director. You need to use an architecture to collaborate and develop your members in parallel. But when you are fighting on your own, the framework like MVC is really unnecessary. Instead, it is inefficient to rely too much on MVC to make metaphysical things.
For social networking websites, mvc cannot solve your problems. Even if you don't need to be object-oriented, all pages use process storage, which is more efficient.
The same is true for object orientation. When you need multiple instances, you can create a class to facilitate operations. When you only need to be an object, the object-oriented approach is meaningless.
Don't think about the framework, mvc or something at the beginning of development. Use the most direct method to make the first version as soon as possible, and then make improvements on demand first, this is the most appropriate method.

Php mvc Mode

Mvc is just a programming idea, regardless of language.

Many php projects use mvc, which is suitable for large-scale project development.

MVC is short for three words: Model, View, and Controller ). The purpose of the MVC model is to achieve the functional division of the Web system. The Model layer implements the business logic in the system, which can usually be implemented using a JavaBean or EJB. The View layer is used for interaction with users. It is usually implemented using JSP. The Controller layer serves as a bridge between Model and View. It can dispatch user requests and select an appropriate View for display, it can also interpret user input and map them to operations that can be performed at the model layer.
 

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.