Home  >  Article  >  Backend Development  >  Detailed explanation of php url pseudo-static process

Detailed explanation of php url pseudo-static process

WBOY
WBOYOriginal
2016-07-25 08:59:411221browse
  1. //Convert url to static url
  2. function url_rewrite($file,$params = array (),$html = "",$rewrite = true)
  3. {
  4. if ($rewrite ) { //Do not rewrite during the development stage. During development, set $rewrite = false
  5. $url = ($file == 'index') ? '' : '/' . $file;
  6. if (!emptyempty ( $params) && is_array($params)) $url .= '/' . implode('/', $params);
  7. if (!emptyempty ($html)) $url .= '.' . $html;
  8. } else {
  9. $url = ($file == 'index') ? '/' : '/' . $file;
  10. if (substr($url, -4) != '.php' && $file != 'index') $url .= '.php';
  11. if (!emptyempty ($params) && is_array($params)) $url .= '?' . http_build_query($params);
  12. }
  13. return $url ;
  14. }
  15. echo url_rewrite('test.php',array('class'=>"User",'act'=>'check','name'=>'tank'));
  16. / When /$rewrite = false, the display is as follows /test.php?class=User&act=check&name=tank
  17. echo url_rewrite('test.php', array ('class'=>"User",'act'= >'check','name'=>'tank'));
  18. //When $rewrite = true, the display is as follows/test.php/User/check/tank
  19. echo url_rewrite('test', array ('class'=>"User",'act'=>'check','name'=>'tank'));
  20. //When $rewrite = true, the display is as follows /test/ User/check/tank
  21. echo url_rewrite('test', array ('class'=>"User",'act'=>'check','name'=>'tank'),'html' );
  22. //When $rewrite = true, the display is as follows /test/User/check/tank.html
  23. ?>
  24. '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:

Copy the code

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

  1. RewriteCond %{REQUEST_FILENAME} !-f //File not found
  2. RewriteCond %{REQUEST_FILENAME} !-d //Cannot find directory
  3. 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

  1. $filename = $_SERVER['REQUEST_URI']; //Requested url
  2. /**Requested url, "/test3/test.php/User/check/tank"
  3. * test.php is the php file to go to
  4. * User is the class name
  5. * check is the method name in the class
  6. * tank is to be passed to check parameters*/
  7. preg_match("/(w+.php)/" ,$filename,$match); //Find the php file name
  8. $array = explode('/',$filename); //Split the static url
  9. $key = array_keys($array,$match[0 ]); //Get the subscript Array corresponding to the file ( [0] => 2 )
  10. $file_array = array_slice($array,0,$key[0]+1); //Array ( [0] = > [1] => test3 [2] => test.php )
  11. $param_array = array_slice($array,$key[0]+1); //Array ( [0] => User [1 ] => check [2] => tank )
  12. $file_path = implode('/',$file_array);
  13. if($array[$key[0]] != "index.php"){
  14. include_once($array[$key[0]]); //Include the php file in the request url, here it is test.php
  15. }
  16. if(class_exists($param_array[0])){ //Judge Check if there is a class User in the file test.php
  17. $obj = new $param_array[0];
  18. if(method_exists($obj,$param_array[1])){ //Check if there is a class User There is no check method
  19. $obj->$param_array[1]($param_array[2]); //Call this method, the result is (my name is tank)
  20. }
  21. }
  22. ?>
Copy the code

5, test.php file

  1. class User {
  2. public function check($name){
  3. echo "My name is".$name;
  4. }
  5. }
  6. ?>
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.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn