This article is just a case of a simple introduction of the PHP implementation of the MVC principle, in fact, most of the framework of MVC is based on this principle to achieve, not much nonsense, directly on the code amount!
I. Structure of the document
Create 3 folders
Controller folder holds director files
View folder holds views file
Model folder holds data files
Establish 1 index.php as the sole entrance
Second, the controller
We set up a democontroller.php file under the Controller folder, the file content is as follows
In this file we just set up an object named Democontroller and contains a method of index, which outputs Hello world.
The index method in Democontroller is executed in index.php below.
The code for index.php is as follows
Run Index.php,ok wish we saw our long-lost Hello world. These two files are very simple, but also reveal a little bit of the nature of MVC, running the controller we want to run through the unique portal. Of course the controller part should be determined by the URI, so let's rewrite the index.php so that he can use the URI to decide to run the controller.
The index.php rewrite code is as follows:
Enter Http://localhost/index.php?c=demo&a=index in the browser and get our Hello world. Of course if we have another controller and want to run it, just modify the values of C and a in the URL parameters.
Here are a few questions to explain.
PHP is a dynamic language, we can directly through the string new out of the object we want and run the method we want, namely the above new $c _name, we can understand into new ' Democontroller ', because $c_ The value of name itself is ' Democontroller ', and of course the direct new ' democontroller ' is not possible, where the ' Democontroller ' string must be brokered by a variable. method is the same.
Second, we in the URL of the value of C is demo, that is, $c_name value should be Democontroller Ah, PHP is not case-sensitive, so also can run? PHP Case-sensitive sentence is not complete, in PHP only variables (preceded by $) and constants (define defined) are case-sensitive, and the class name, Dharma name even some keywords are case-insensitive. True,false,null and so on can only be capitalized or all lowercase. Of course we'd better be case-sensitive in the actual coding process.
Third, view
We just output a "Hello World" in the controller above, and do not achieve the effect of MVC, below I will add the view on this basis, I believe here you can basically think of how to add the View function. Yes, it is done through the require of evil or by the include.
First we create a index.php in the View folder, write something (hehe, I write Hello World). Then we rewrite our previous democontroller.
The code is as follows:
/* End of File democontroller.php */
Run it in the browser again to see if we've already exported what we want.
Then we pass some data to the view through the controller, and the code looks like this:
/* End of File democontroller.php */
The index.php file code under the View folder is as follows:
Demo
echo $item. '