A brief description of MVC theory and how PHP realizes MVC

Source: Internet
Author: User
Keywords PHP

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

I believe there are many such articles, but today I would like to share my experience with you. Pure original, I also have no reservations, I hope to help the novice, there are said the wrong place, also welcome to point out.

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.

Why 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;

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.

The idea of implementing MVC in PHP

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.

I provide you with an MVC base class design idea for reference only:

1. Design of Controller class

A main () method for program invocation, mainly through get and post variables to determine how to handle.

A Getmodel ($model) method that calls the directory's m when it needs to call the database.

A display ($view) method, called in the Main () method, loads the corresponding V, and drops the main () method of V;

The design of the 2.View class 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 it needs to call the database.

A display ($template) that invokes the corresponding template file and passes the data to the template.

Design of the 3.Model class

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, for example 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.

This article is not soft, writing is the actual things, I hope to help beginners, if you do not understand, I am sorry, my writing is limited.

Reprint please indicate the source author: Liu Dongxin (Bitpart) original address: http://www.wodianer.net/article_52_PHP-MVC.html

Related Article

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.