PHP is now widely used as a programming language, as long as PHP development is inseparable from a number of PHP framework, the development framework provides us with a flexible way to develop, MVC layer separation, business decoupling and so on, so that our development more quickly and easily. But many people just use the framework, but never understand how the framework is implemented internally, which is of little use to our ability to program. So in this article, we'll start with a simple feature, that is to say, the general single-entry frame of the MVC framework of the implementation method of the route. Frame routing is the same structure: LOCALHOST/MVC/INDEX.PHP/CLASSNAME/FUNCTIONNAME/VAR1/VAR2. The index.php here is often referred to as 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. Here we will simply implement the MVC framework routing functionality.
1, first, build a good MVC file structure
Now let's try it out, how can we access the files inside the controllers.
Enter the following in the index.php:
<?php /** * MVC routing function Simple implementation * @desc simple implementation of MVC routing function * $Author: Zhihua_w * // print out all the server variables Print_r ($_server); ? >
Then enter the access address to try. Here I am using the local environment, I visited the address is: LOCALHOST/MVC/INDEX.PHP/CLASS/FUNCTION/VAR1. Below I post two most important variables:
[Request_uri] =/mvc/index.php/class/function/var1 [Script_name] =/mvc/index.php
2. index.php File contents
<?php /** * MVC routing function Simple implementation * @desc simple implementation of MVC routing function * $Author: Zhihua_w * // /define Application path define (' APPPATH ', trim (__dir__). '/')); Obtain the requested address $root = $_server[' script_name '); $request = $_server[' Request_uri '); $URI = Array (); Get the address $url = Trim (Str_replace ($root, ', $request), '/') after index.php; If empty, the access root address if (empty ($url)) { //default controller and default method $class = ' index '; $func = ' welcome '; } else { $URI = explode ('/', $url); If function is null, the default access is index if (count ($URI) < 2) { $class = $URI [0]; $func = ' index '; } else { $class = $URI [0]; $func = $URI [1]; } } Load the class in the include (APPPATH. '/' . ' Application/controllers/'. $class. '. php '); Instantiation, capitalize the first letter of the controller $obj = Ucfirst ($class); Call_user_func_array ( //Call internal function Array ($obj, $func), //pass parameter array_slice ($URI, 2) ); ? >
3, add the following 2 files in Application/controllers (index.php,hello.php)
①index.php
<?php * MVC routing function Simple implementation * @desc simple implementation of MVC routing function * $Author: Zhihua_w */ class Index { function Welcome () { # code ... echo "Default covtroller!"; } ? >
②hello.php
<?php/** * MVC routing function Simple implementation * @desc simple implementation of MVC routing function * $Author: Zhihua_w */ class hello{ Public Function Index () { echo "Hello world!"; } Public Function name ($name) { echo "hello". $name; } } ? > ] View plain copy<?php /**
4. Testing
Test it and see if you can access it. Based on the routing structure above. We can try that. Through the test we will find that the access is normal, the correct invocation of the class inside the name of Hello, and then the parameter Jack passed. Try not to enter the function name, see if you can call index by default, the answer is also possible. The last one is to visit the root address to see that it is also correctly mapped to the default controller. (The test result picture is not posted.) )
The above case has been explained so that a simple MVC routing function is complete.