Php creates its own MVC framework _ PHP Tutorial

Source: Internet
Author: User
Php creates its own MVC framework. I. file structure creation 3 folders controller folder storage controller file view folder storage view file model folder storage data file creation 1 index. php as the only entry 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:

The code is as follows:


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:

The code is as follows:


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:

The code is as follows:


$ 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:

The code is as follows:


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:

The code is as follows:


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:

The code is as follows:




Demo



Foreach ($ data ['list'] as $ item)
{
Echo $ item .'
';
}
?>


Creating three folders: controller folder storage controller file view folder storage view file model folder storage data file creation one index. php as the only entry...

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.