An analysis of MVC programming idea in PHP program design, _php tutorial of MVC Programming thought

Source: Internet
Author: User

An analysis of MVC programming Thought in PHP program design and the idea of MVC programming


PHP's MVC programming ideas have been widely used in the development of large-scale projects, many mature MVC framework is also gradually known and widely used in various projects, more common such as thinkphp, CodeIgniter, Symfony, Yii, CakePHP and so on. This article is to briefly describe the MVC program design idea of PHP.

First, what is MVC

Simple to say is the site source code classification, layering.
MVC three-letter meaning:
M:model model, responsible for database operations.
The V:view view is responsible for invoking the model data and then invoking the template to show the final effect.
The C:controller controller, the entry of the program, decides which view to call, and tells the view what to do.
In this case, the order of execution of the program is C-V-M or C-M, and the name of MVC is the opposite.

Second, why MVC

1. can make the website program physical structure more reasonable.

When using PHP to build a Web site, the most stupid way, you may put each page to build a PHP file. If your site only index.php,menu.php.article.php three pages, then you can not use MVC, but we do the general site, often dozens of pages, put all the pages in the root directory is obviously not what we can accept, So you need a reasonable idea to classify your code, divide them into different directories by function, and load calls by program intelligence, which is what MVC will help you with.

2. make the code easier to maintain.

We look at a single page, the most stupid way, is the PHP code and HTML code mix, which is obviously not good enough, you have to maintain the site when you have to distinguish between PHP, where is the HTML, which for a programmer, it is a disaster. So many people use smarty, so that can be "data processing" and "page display" separate, this is really good, there are many people are doing so, but this is not mvc,mvc to do is to "data processing" is divided into "logical processing" and "Database Operations", which is said to be layered.
So when your program is wrong or want to modify, it becomes very easy, when the page display error, you go to check the V or template file, when the logic is a problem, you go to check C and V, when your database operation error to check M.
In fact, MVC generally want to divide a page of PHP into 4 pages, respectively, c,v,m, template. Their respective responsibilities, convenient management.

3. facilitates code reuse.

MVC will generally put a large function in a directory, that is, by a C to manage.
For example, to make a website with Membership system, we can put the member-related code into the user directory, by User_controller Unified management, when our other site also needs the membership system, we can directly copy the directory past, modify the interface can be.

Third, the idea of implementing MVC in PHP

We need three base classes: Controller, View, Model, then different C, V, and M inherit their corresponding properties and methods, if you do not understand here, you can go to look at the object-oriented book.

Here you will find a basic class of MVC design ideas, for reference only:

1. design of controller class

A main () method, which is intended for program invocation, is primarily determined by the get and post variables.
A Getmodel ($model) method that calls the corresponding directory's m when the database needs to be called.
A display ($view) method, called in the Main () method, loads the corresponding V, and falls off the main () method of V;

2.view class design is similar to controller

A main () method that calls this method when C loads V, so that the program can continue to execute.
A Getmodel ($model) method that calls the corresponding directory's m when the database needs to be called.
A display ($template) that invokes the corresponding template file and passes the data to the template.

3.design of model class

You can define properties such as how to manipulate those tables, manipulate those fields, and so on.
A Getdb () method to obtain an instance of a database class (the database class is generally designed in single-piece mode)
A load () method that loads a data.
An Add () method that automatically constructs an SQL statement based on a defined property and performs an insert operation.
A Eidt () method, as above, but performs a modification operation.
A del () method, ditto, but performs a delete operation.
To make it easier for novices to understand how this idea works, let's now simulate a user login scenario and see how MVC works.
Now suppose that all the data is submitted to index.php,

The first step:
We submit each get variable to tell index.php which C to use, for example, can this index.php? Controller=user
Then index receives the get variable and nothing needs to be done to find/user/directly controller.php, throw all the data to him, originally get and post is the global, so index.php also do not need to do, directly call C's main function can be, to this index.php task completed.

Step Two:
C's main function starts execution, checks the variable, discovers the user to perform the login operation (very simple, you post the variable do=login to be possible), then calls Getmodel, loads the corresponding m class (for example/user/models/model.php), and instantiates, Call the instance's Load method, load the user's data, determine whether the user submitted a password consistent, if the submitted data is incorrect header jumps to the error page, if correct, call the display () method, load the corresponding V (for example,/user/views/ details.php), and instantiate, call its main () function and enter the third step. The task to this c is completed, and the second operation is done in the main function.

Step Three:
You can choose to call Getmodel () to load m, rewrite the data, or in C to instantiate V, pass the parameters (such as session), when V has determined to get the data, display (), loading the template, MVC execution is complete.
Of course, because of the number of words and energy limitations, here is only a very brief summary, the actual implementation of the time to consider a lot of details, but I design mvc, the approximate idea is this, also used in the actual, feel good.


What is the important website programming idea of PHP?

First, you need to figure out what MVC is for, and the role of MVC is in collaborative development, like you have a development team. You are the director, you need to use a framework to allow your team members to work together, parallel development. And when you're fighting alone, there's really no need for a framework like MVC. Instead, it is inefficient to rely too much on MVC to finally make metaphysical things.
Social networking Sites Oh, MVC can't solve your problem, even if you don't have to object-oriented, all pages are stored in the process, the efficiency is higher.
The same is true of object-oriented, when you need multiple instances, you get a class, easy to operate, when you only need to be an object, that object-oriented instead of meaning.
Do not think about this framework at the beginning of development, MVC or something, in the most direct way, the fastest to make the first version, and then in the demand-oriented improvement, this is the most appropriate method.

Help PHP MVC pattern

MVC is just a programming idea, not a language.

There are many projects in PHP development that use MVC, which is suitable for large project development

MVC is the abbreviation for three words: Model, view, and control controller, respectively. The purpose of the MVC pattern is to realize the functional division of the web system. The model layer implements the business logic in the system, which can often be implemented with JavaBean or EJBS. The view layer is used for interaction with the user and is usually implemented with JSP. The controller layer is a bridge between model and view that can dispatch a user's request and select the appropriate view for display, and it can also interpret the user's input and map them to actions that the model layer could perform

http://www.bkjia.com/PHPjc/851341.html www.bkjia.com true http://www.bkjia.com/PHPjc/851341.html techarticle On the concept of MVC programming in PHP programming, the idea of MVC programming in PHP has been widely used in the development of various large-scale projects, and many mature MVC frameworks have gradually been ...

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