I. File structure
Create 3 folders
The controller folder stores controller files.
View folder stores view files
Model folder stores data files
Create an index. php as the only entry
Ii. Controller
Create a democontroller. php file in the controller folder. The file content is as follows:
Copy codeThe Code is as follows:
<? Php
Class DemoController
{
Function index ()
{
Echo ('Hello World ');
}
}
/* End of file democontroller. php */
In this file, we just created an object named DemoController and included an index method. This method outputs hello world. Next, run the index method in DemoController in index. php.
The index. php code is as follows:
Copy codeThe Code is as follows:
<? Php
Require ('controller/democontroller. php ');
$ Controller = new DemoController ();
$ Controller-> index ();
/* End of file index. php */
Run index. php. OK. We can see our long-time hello world. These two files are very simple, but they also reveal the essence of a little bit of mvc and run the controller we want to run through the only entry. Of course, the controller part should be determined by uri, so let's rewrite index. php so that it can use uri to decide which controller to run.
The index. php rewrite code is as follows:
Copy codeThe Code is as follows:
<? Php
$ C_str = $ _ GET ['C'];
// Obtain the controller to run
$ C_name = $ c_str. 'controller ';
// The Name Of The controller obtained in the url according to the conventions does not contain the Controller.
$ C_path = 'controller/'. $ c_name.'. php ';
// According to the Conventions, the controller file must be created in the controller folder. The class name must be the same as the file name, and all file names must be in lowercase.
$ Method = $ _ GET ['a'];
// Obtain the action to run
Require ($ c_path );
// Load the controller File
$ Controller = new $ c_name;
// Instantiate the controller File
$ Controller-> $ method ();
// Run the action under the instance
/* End of file index. php */
Enter http: // localhost/index. php In the browser? C = demo & a = index and get our hello world. Of course, if we have another controller and want to run it, we only need to modify the values of c and a in the url parameters.
Here are a few questions to explain.
I. php is a dynamic language. We can use the new string to get the desired object and run the method we want, that is, the new $ c_name above, we can understand it as new 'democontroller', because the value of $ c_name itself is 'democontroller'. Of course, it is impossible to write new 'democontroller' directly, the 'democontroller' string must pass through a variable. The method is the same.
2. The value of c in the url is demo, that is to say, the value of $ c_name should be demoController. Isn't php case-sensitive? Can it run like this? The case-sensitive php statement is incomplete. in php, only variables (with $ in front) and constants (defined by define) are case-sensitive, while class names, the legal name and even some keywords are case insensitive. True, false, null, and so on can only be in upper or lower case. Of course, we 'd better differentiate the case in the actual encoding process.
3. View
In the previous controller, we only output a "hello world", which does not achieve the mvc effect. Next I will add the view function on this basis, I believe that you can basically think of how to add a view. Yes, it is implemented through require or include.
First, create an index. php file in the view folder and write anything (Haha, I wrote hello world ). Then we will rewrite our previous DemoController. The Code is as follows:
Copy codeThe Code is as follows:
<? Php
Class DemoController
{
Function index ()
{
Require ('view/index. php ');
}
}
/* End of file democontroller. php */
Run it in the browser to see if the content we want has been output.
Then we can pass some data to the view through the controller. The Code is as follows:
Copy codeThe Code is as follows:
<? Php
Class DemoController
{
Function index ()
{
$ Data ['title'] = 'first title ';
$ Data ['LIST'] = array ('A', 'B', 'C', 'D ');
Require ('view/index. php ');
}
}
/* End of file democontroller. php */
The code for the index. php file in the view folder is as follows:
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> demo </title>
</Head>
<Body>
<H1> <? Php echo $ data ['title'];?> </H1>
<? Php
Foreach ($ data ['LIST'] as $ item)
{
Echo $ item. '<br> ';
}
?>
</Body>
</Html>