Php mvc Framework routing learning notes, mvc Framework routing learning notes
When talking about PHP web development, we naturally cannot do without the development framework. The Development Framework provides us with flexible development methods, MVC layer separation, and business decoupling...
The first article is a simple one. Let's talk about the routing function of the MVC framework...
Generally, a single-entry framework route entry adopts the following structure:
Domain/index. php/classname/functionname/var1/var2
Here index. php is called the entry file... For the server, you only access the controller and the method in index. php, and even the value passing is implemented in the framework based on the PHP layer.
Talk is cheap, show you the code !!
First, set up the following file structure
Let's try to see how to access the files in controllers...
Enter the following content in index. php:
Print_r ($ _ SERVER );
Visit the following address.
Yourdomain/index. php/class/function/var1
Here, I use a local environment. The address I access is localhost/MVC/index. php/class/function/var1.
I posted the two most important variables.
[REQUEST_URI] =>/MVC/index. php/class/function/var1
[SCRIPT_NAME] =>/MVC/index. php
In fact, the most basic principle of routing is here:
These two variables are used to extract the class, function, and parameters in the url address, and then include the class. The callback function call_user_func_array of PHP is used to call the corresponding function and pass the corresponding parameters.
Next, the code should be easier to read than I write. Haha ~~
The content of index. php is as follows:
<? Php # define the application Path define ('apppath', trim (_ DIR __,'/')); # obtain the request address $ root = $ _ SERVER ['script _ name']; $ REQUEST = $ _ SERVER ['request _ URI ']; $ URI = array (); # obtain the index. url after php $ url = trim (str_replace ($ root, ", $ request), '/'); # If it is empty, is to access the root address if (empty ($ url) {# default controller and default method $ class = 'index'; $ func = 'Welcome ';} else {$ URI = explode ('/', $ url); # if function is empty, index if (count ($ URI) <2) is accessed by default) {$ class = $ URI [0]; $ func = 'index';} else {$ class = $ URI [0]; $ func = $ URI [1];} # load the class into include (APPPATH. '/'. 'application/controllers /'. $ class. '. php '); # instantiate $ obj = new ucfirst ($ class); call_user_func_array (# Call the internal function array ($ obj, $ func), # pass the parameter array_slice ($ URI, 2 ));
Add the following two files to application/controllers:
Index. php is used as the default controller.
<?php class Index { function welcome() { echo ‘I am default controller'; } } ?> hello.php<?php class Hello { public function index() { echo ‘hello world'; } public function name($name) { echo ‘hello ‘ . $name; } } ?>
Test to see if you can access the service. According to the preceding Routing Structure. Let's try it.
The access is normal. The name method in the class hello is called correctly, and the barbery parameter is passed...
Try again without entering the function name to see if index can be called by default ..
The answer is yes, too...
Last one, visit the root address.
It is also mapped to the default controller...
OK, a simple MVC routing function is complete...
Articles you may be interested in:
- CodeIgniter php mvc Framework Chinese website
- MayFish php mvc Architecture Development Framework
- Php creates its own MVC Framework
- Usage of Smarty based on PHP Web development MVC Framework
- Analysis on the implementation principle of php mvc mode (a simple MVC Framework example)
- Php implementation of the simplest MVC Framework example tutorial