- Convert URLs to static URLs
- function Url_rewrite ($file, $params = Array (), $html = "", $rewrite = True)
- {
- if ($rewrite) {//development phase is not rewrite, where the development of the time, put $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 '));
- $rewrite = False, the following/test.php?class=user&act=check&name=tank is displayed
- echo url_rewrite (' test.php ', Array (' class ' = "User", ' act ' = ' check ', ' name ' = ' tank '));
- $rewrite = True, the following/test.php/user/check/tank are displayed
- echo url_rewrite (' Test ', Array (' class ' = "User", ' act ' = ' check ', ' name ' = ' tank '));
- $rewrite = True, the following/test/user/check/tank are displayed
- echo url_rewrite (' Test ', Array (' class ' = "User", ' act ' = ' check ', ' name ' = ' tank '), ' HTML ');
- $rewrite = True, the following/test/user/check/tank.html are displayed
- ?>
- "User", ' act ' = ' check ', ' name ' = ' tank ')); > ">test
Copy CodeThe above simply write a method to convert the dynamic URL into a static URL, the page will generate links as follows:
- Test
Copy CodeIf you click here directly, you will be quoted 404 errors, because the root cannot find tank this directory. The difficulty is here too, so we have to specify a PHP file for directories and files that cannot be found. This should be used to Apache,nginx, or htaccess and so on. Third, specify a unified portal
- Rewritecond%{request_filename}!-f//File not found
- Rewritecond%{request_filename}!-d//cannot hit directory
- Rewriterule. /test3/index.php [L]
Copy CodeWhether you do it in a. htaccess way, or in a configuration file such as Apache, it is possible. What does the above three sentences mean, if you can't find the directory to go to the index.php file, and if you can't find the file, go to index.php. In doing so, when we visit the Http://localhost/test3/test.php/User/check/tank, we will go to index.php, since we know the PHP file, it is good to do. The following things are done in the same way as Http://localhost/test3/test.php/User/check/tank, and the other way around. IV. index.php file
- $filename = $_server[' Request_uri '); The requested URL
- URL of the/** request, "/test3/test.php/user/check/tank"
- * test.php the PHP file to go to
- * User is class name
- * Check is the method name in class
- * Tank is the parameter to be passed to check */
- Preg_match ("/(\w+\.php)/", $filename, $match); Find PHP file names
- $array = explode ('/', $filename); To split a 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]]); The package letter requests the PHP file in the URL, here is test.php
- }
- if (Class_exists ($param _array[0])) {//Determine test.php This file has the user class
- $obj = new $param _array[0];
- if (Method_exists ($obj, $param _array[1])) {//Determine if the user has check in this class
- $obj $param _array[1] ($param _array[2]); Call this method, the result is (my name is called tank)
- }
- }
- ?>
Copy CodeFive, test.php file
- Class User {
- Public function Check ($name) {
- echo "My name is called". $name;
- }
- }
- ?>
Copy CodeHere, when we visit Http://localhost/test3/test.php/User/check/tank. The results are as follows: My name is tank, and the address bar remains static. |