Analysis of MVC programming thought _php skill in PHP programming

Source: Internet
Author: User
Tags php programming

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

One, what is MVC

Simply put, the site source code classification, layering.
MVC three-letter meaning:
M:model model, responsible for database operations.
V:view View, is responsible for calling the model to fetch data, and then call the template, showing the final effect.
C:controller Controller, the entry of the program, decide which view to call, and tell the view what to do.
in this case, the sequence of execution of the program is C-V-M or C-M, and the name of MVC is just the opposite.

Second, why should MVC

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

When building a Web site in PHP, the dumbest way you can build a PHP file for each page. If your site only index.php,menu.php.article.php three pages, then you can not MVC, but we do a general site, often dozens of pages, put all the pages in the root directory is obviously not acceptable to us, So you need a reasonable mind to categorize your code, divide them into different directories by function, and load calls by program intelligence, which is what MVC will help you do.

2. make code easier to maintain.

We look at a single page, the stupidest 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 where is PHP, where is HTML, which for a programmer, is simply a disaster. So many people use smarty, so that "data processing" and "page display" can be separated, 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 called layering.
So when your program is wrong or want to change, it becomes very easy, when the page shows the error, you go to check V or template files, when the logic is problematic, you check C and V, when you database operation error to check M.
In fact, MVC generally wants to split a page of PHP into 4 pages, respectively, c,v,m, templates. Perform their duties and facilitate management.

3. useful for code reuse.

MVC will generally put a large function in a directory, that is, a C to manage.
For example, to do a website containing membership system, we can put the member-related code into the user directory, by User_controller Unified management, when we another site also need membership system, we can directly copy the directory past, modify the interface on it.

Three, PHP implementation of the idea of MVC

We need three base classes: Controller, View, Model, and then different C, V, m to inherit they have corresponding attributes and methods, if you do not understand here, you can go to see the object-oriented book.

Here to provide you with an MVC base class design ideas, for reference only:

1. design of controller class

A main () method for program invocation, mainly through get and post variables to determine what to do with.
A Getmodel ($model) method that calls the directory's M when the database needs to be invoked.
A display ($view) method, called in the Main () method, loads the corresponding V, and drops the main () method of V;

2.view class design is similar to controller

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

3.Model class Design

You can define properties, such as manipulating those tables, manipulating those fields, and so on.
A Getdb () method to obtain an instance of a database class (database classes are generally designed in a single piece mode)
A load () method that loads a data.
An Add () method that automatically constructs an SQL statement and performs an insert operation, based on a defined property.
A Eidt () method, ditto, but performs a modification operation.
A del () method, ditto, but performs a delete operation.
In order to enable beginners to better understand the workings of my idea, we now simulate a user login scenario to see how MVC works.
Now suppose that all the data is submitted to index.php,

First step:
We submit each get variable and tell index.php which c to use, such as index.php? Controller=user
Then index receives the get variable, nothing to do, directly to find/user/ controller.php, throw all the data to him, originally get and post is global, so index.php also do not need to do anything, directly call the main C function on it, to this index.php task completed.

Step Two:
C's main function starts executing, examines the variable, discovers the user to perform the login operation (very simple, you post a variable do=login to be possible), then calls the 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 to determine whether the user submitted the same password, if the submitted data incorrect header jump 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 tasks to this C are complete, and the second does not operate in the main function.

Step Three:
You can optionally invoke Getmodel () load m, rewrite the fetch data, or pass the parameter (for example, session) when C instantiates V, and when V has been determined to get the data, display (), load the template, and execute MVC.
Of course, because of the word 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, probably the idea is this, also used in the actual, feel good.

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.