Home > Backend Development > PHP Tutorial > Learn to implement mvc simple framework with php

Learn to implement mvc simple framework with php

WBOY
Release: 2016-07-25 09:05:31
Original
1054 people have browsed it
  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 */
Copy code

The above file just creates an object named DemoController and contains an index method, which outputs hello world. Next, execute 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 */
Copy the code

Run index.php, you can see the output hello world. The essence of mvc is to run the controller we want to run through the only entrance. Of course, the controller part should be determined by the uri, so let's rewrite index.php so that it can determine which controller to run through the uri. Modify index.php code:

  1. $c_str=$_GET['c'];
  2. //Get the controller to run
  3. $c_name=$c_str.'Controller';
  4. //Get it from the url as agreed The controller name does not contain Controller, so fill it in here.
  5. $c_path='controller/'.$c_name.'.php';
  6. //According to the agreement, the controller file must be created in the controller folder, the class name must be the same as the file name, and the file name must be all lowercase.
  7. $method=$_GET['a'];
  8. //Get the action to be 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 this instance
  15. /* End of file index.php */
Copy the code

Enter http://localhost/index in the browser. php?c=demo&a=index, we got 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 instructions: 1. PHP is a dynamic language. We can directly get the object we want and run the method we want through the string new, that is, the new $c_name above, we can understand it as new 'DemoController', because the value of $c_name itself It's 'DemoController'. Of course, writing new 'DemoController' directly like this won't work. The 'DemoController' string must be transferred through a variable. The method is the same.

2. The value of c in our URL is demo, which means the value of $c_name should be demoController. Isn’t PHP case-sensitive? Can it run like this? The sentence "php is case-sensitive" is incomplete. In php, only variables (preceded by $) and constants (defined by define) are case-sensitive, while class names, method names and even some keywords are not case-sensitive. written. And true, false, null, etc. can only be all uppercase or all lowercase. Of course we'd better be case-sensitive during the actual encoding process.

3. View We only output a "hello world" in the previous controller, which did not achieve the effect of mvc. Next, I will add the view function on this basis. I believe that by now everyone can basically think of how to add the view function. Yes, it is achieved through the evil require or include. First, we create an index.php under the view folder and write anything (haha, I still 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 */
Copy the code

and run it in the browser to see if the content we want has been output. Then we pass some data to the view through the controller to see:

  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 */
Copy code

view index.php file in the folder

  1. demo
  2. foreach ($data['list'] as $item)
  3. {
  4. echo $item.'
    ';
  5. }
  6. ?> ;
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template