-
- //Convert url to static url
- function url_rewrite($file,$params = array (),$html = "",$rewrite = true)
- {
- if ($rewrite ) { //Do not rewrite during the development stage. During development, set $rewrite = false
- $url = ($file == 'index') ? '' : '/' . $file;
- if (!emptyempty ( $params) && is_array($params)) $url .= '/' . implode('/', $params);
- if (!emptyempty ($html)) $url .= '.' . $html;
- } else {
- $url = ($file == 'index') ? '/' : '/' . $file;
- if (substr($url, -4) != '.php' && $file != 'index') $url .= '.php';
- if (!emptyempty ($params) && is_array($params)) $url .= '?' . http_build_query($params);
- }
-
- return $url ;
- }
-
- echo url_rewrite('test.php',array('class'=>"User",'act'=>'check','name'=>'tank'));
- / When /$rewrite = false, the display is as follows /test.php?class=User&act=check&name=tank
-
- echo url_rewrite('test.php', array ('class'=>"User",'act'= >'check','name'=>'tank'));
- //When $rewrite = true, the display is as follows/test.php/User/check/tank
-
- echo url_rewrite('test', array ('class'=>"User",'act'=>'check','name'=>'tank'));
- //When $rewrite = true, the display is as follows /test/ User/check/tank
-
- echo url_rewrite('test', array ('class'=>"User",'act'=>'check','name'=>'tank'),'html' );
- //When $rewrite = true, the display is as follows /test/User/check/tank.html
- ?>
- 'check','name'=>'tank'));?>">test
-
Copy the code
The above simply writes a method to convert the dynamic URL into a static URL. The link will be generated in the page as follows:
If you click directly here, it will definitely A 404 error is reported because the root cannot find the tank directory. The difficulty lies here, so we need to specify a php file for the directories and files that cannot be found. This requires using apache, nginx, or htaccess, etc.
Three, designate a unified entrance
-
- RewriteCond %{REQUEST_FILENAME} !-f //File not found
- RewriteCond %{REQUEST_FILENAME} !-d //Cannot find directory
- RewriteRule . /test3/index.php [L]
Copy code
Whether you implement it in .htaccess or write it in a configuration file such as apache, it is all possible. What do the above three sentences mean? If the directory cannot be found, go to the index.php file. If the file cannot be found, go to index.php.
After doing this, when we visit http://localhost/test3/test.php/User/check/tank, it will be transferred to index.php. Now that we know the php file, it will be easy to handle.
The following content is all operated by rewriting http://localhost/test3/test.php/User/check/tank, and other methods are similar.
Four, index.php file
-
- $filename = $_SERVER['REQUEST_URI']; //Requested url
-
- /**Requested url, "/test3/test.php/User/check/tank"
- * test.php is the php file to go to
- * User is the class name
- * check is the method name in the class
- * tank is to be passed to check parameters*/
-
- preg_match("/(w+.php)/" ,$filename,$match); //Find the php file name
-
- $array = explode('/',$filename); //Split the static url
-
- $key = array_keys($array,$match[0 ]); //Get the subscript Array corresponding to the file ( [0] => 2 )
- $file_array = array_slice($array,0,$key[0]+1); //Array ( [0] = > [1] => test3 [2] => test.php )
- $param_array = array_slice($array,$key[0]+1); //Array ( [0] => User [1 ] => check [2] => tank )
-
- $file_path = implode('/',$file_array);
-
- if($array[$key[0]] != "index.php"){
- include_once($array[$key[0]]); //Include the php file in the request url, here it is test.php
- }
-
- if(class_exists($param_array[0])){ //Judge Check if there is a class User in the file test.php
-
- $obj = new $param_array[0];
- if(method_exists($obj,$param_array[1])){ //Check if there is a class User There is no check method
- $obj->$param_array[1]($param_array[2]); //Call this method, the result is (my name is tank)
- }
- }
- ?>
Copy the code
5, test.php file
-
- class User {
- public function check($name){
- echo "My name is".$name;
- }
- }
- ?>
-
Copy code
Here we go, when we visit http://localhost/test3/test.php/User/check/tank.
The result is as follows: my name is tank, and the address bar remains static.
|