Learning to use php to implement a simple mvc framework

Source: Internet
Author: User
Learning to use php to implement a simple mvc framework

  1. /**
  2. Controller:
  3. @ Link jbxue.com
  4. */
  5. Class DemoController
  6. {
  7. Function index ()
  8. {
  9. Echo ('Hello World ');
  10. }
  11. }
  12. /* End of file democontroller. php */

The above file only creates an object named DemoController and contains an index method, which outputs hello world. Next, run the index method in DemoController in index. php. File: index. php

  1. Require ('Controller/democontroller. php ');
  2. $ Controller = new DemoController ();
  3. $ Controller-> index ();
  4. /* End of file index. php */

Run index. php to view the output hello world. The essence of mvc is to run the controller we want to run through a unique 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. Modify the index. php code:

  1. $ C_str = $ _ GET ['c'];
  2. // Obtain the controller to run
  3. $ C_name = $ c_str. 'controller ';
  4. // The name of the controller obtained in the url according to the conventions does not contain the Controller.
  5. $ C_path = 'Controller/'. $ c_name.'. php ';
  6. // 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.
  7. $ Method = $ _ GET ['A'];
  8. // Obtain the action to run
  9. Require ($ c_path );
  10. // Load the controller file
  11. $ Controller = new $ c_name;
  12. // Instantiate the controller file
  13. $ Controller-> $ method ();
  14. // Run the action under the instance
  15. /* 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.

Related description: 1. 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 we only output a "hello world" in the previous controller, but it 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 rewrite the previous DemoController.

  1. Class DemoController
  2. {
  3. Function index ()
  4. {
  5. Require ('View/index. php ');
  6. }
  7. }
  8. /* 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:

  1. Class DemoController
  2. {
  3. Function index ()
  4. {
  5. $ Data ['title'] = 'first title ';
  6. $ Data ['list'] = array ('A', 'B', 'C', 'D ');
  7. Require ('View/index. php ');
  8. }
  9. }
  10. /* End of file democontroller. php */

Index. php file in the view folder

  1. Demo
  2. Foreach ($ data ['list'] as $ item)
  3. {
  4. Echo $ item .'
    ';
  5. }
  6. ?>

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.