PHP MVC Framework Routing Learning notes, MVC Framework Routing Learning Notes
Referring to the PHP development of the web, nature is inseparable from the development framework, the development framework provides us with a flexible way to develop, MVC layer separation, business decoupling, etc...
First of all, let's talk about the MVC framework's routing capabilities ...
General single-entry frame routing is the same structure:
Domain/index.php/classname/functionname/var1/var2
The index.php here is called the entry file ... For the server, you only have access to the controller and the methods inside the index.php, and even the values are implemented within the framework based on the PHP level.
Talk was cheap, show you the code!!
First of all, build the following file structure first
Let's try it out, how to access the files inside the controllers ...
Enter the following in the index.php
Print_r ($_server);
Then visit the following address to try it.
Yourdomain/index.php/class/function/var1
Here the author I am using the local environment, I visited the address is localhost/mvc/index.php/class/function/var1
I'm sticking out the 2 most important variables.
[Request_uri] =/mvc/index.php/class/function/var1
[Script_name] =/mvc/index.php
In fact, the basic principle of routing is here:
Through these 2 variables to extract the URL address of class and function, parameters, etc., and then the class include in, through the PHP callback function Call_user_func_array call the corresponding function and pass the corresponding parameters.
The next code, read the code should be more understandable than I write. haha ~ ~
The contents of index.php are as follows
Add the following 2 files to the Application/controllers
Index.php used as the default controller
<?php class Index {function welcome () {echo ' I am default controller ';}}?> hello.php<?php class Hello {publ IC function Index () {echo ' Hello World ',} public Function name ($name) {echo ' hello '. $name;}} ?>
Test it and see if you can access it. Based on the routing structure above. Let's try.
This access is normal, correctly called the "Hello" class inside the name method, and then pass the parameter Barbery passed ...
Try not to enter function name and see if you can call index by default:
The answer is yes ...
Last one, visit the root address to see
Also correctly mapped to the default controller ...
OK, a simple MVC routing function is complete ...
Articles you may be interested in:
- CodeIgniter PHP MVC Framework China website
- Mayfish Development Framework for PHP's MVC architecture
- PHP builds your own MVC framework
- Smarty usage Instructions for the MVC framework based on PHP web development
- PHP's MVC pattern Implementation Principle analysis (a simple example of an MVC framework)
- PHP implementation of the simplest MVC Framework Example Tutorial
http://www.bkjia.com/PHPjc/1105375.html www.bkjia.com true http://www.bkjia.com/PHPjc/1105375.html techarticle PHP MVC Framework Routing Learning notes, MVC Framework Routing Learning notes refer to PHP development web, naturally inseparable from the development framework, the development framework provides us with a flexible development approach, MVC layer separation, ...