The article focuses on the use of PHPMVC framework routing. if you need to know how to develop PHP web, you can't do without the development framework. The Development Framework provides us with flexibility.
This article mainly describes how to use php mvc framework routing. if you need to know about it, please refer to it. speaking of PHP web development, we naturally cannot leave 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 index. php is called an entry file... for the server, you only access index. php calls the controller and the method in it, and even transmits values based on the PHP layer in the framework.
Talk is cheap, show you the code !!
First, set up the following file structure. let's try it. how can we access the file in controllers... input the following content in index. php:
Print_r ($ _ SERVER );
Visit the following address to try.
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 index. php instance code is as follows:
-
- # Define the application path
- Define ('apppath', trim (_ DIR __,'/'));
- # Obtain the request address
- $ Root = $ _ SERVER ['script _ name'];
- $ Request = $ _ SERVER ['request _ URI '];
- $ URI = array ();
- # Obtain the address after index. php
- $ Url = trim (str_replace ($ root, ", $ request ),'/');
- # If it is null, it is the access root address
- If (emptyempty ($ url ))
- {
- # Default controller and default method
- $ Class = 'index ';
- $ Func = 'Welcome ';
- }
- Else
- {
- $ URI = explode ('/', $ url );
- # If function is empty, index is accessed by default.
- If (count ($ URI) <2)
- {
- $ Class = $ URI [0];
- $ Func = 'index ';
- }
- Else
- {
- $ Class = $ URI [0];
- $ Func = $ URI [1];
- }
- }
- # Load the class
- Include (APPPATH. '/'. 'application/controllers/'. $ class.'. php ');
- # Instantiation
- $ Obj = new ucfirst ($ class );
- Call_user_func_array (
- # Calling internal functions
- Array ($ obj, $ func ),
- # Passing parameters
- Array_slice ($ URI, 2)
- );
Add the following two files to application/controllers. index. php is used as the default controller.
The instance code is as follows:
-
- Class Index
- {
- Function welcome ()
- {
- Echo 'I am default controller ';
- }
- }
- ?>
-
- Hello. 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 above routing structure. let's try to see if the access is normal. the name method inside the class hello is correctly called, and the barbery parameter is passed... try again without entering function name to see if index can be called by default. the answer is also acceptable... finally, access the root address to see if it is correctly mapped to the default controller... OK, a simple MVC routing function is complete...