Home  >  Article  >  Backend Development  >  Answer several questions on PHPCHINA: URL mapping_PHP tutorial

Answer several questions on PHPCHINA: URL mapping_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:57:50795browse

After the PHPCHINA server was relocated, I basically couldn't access it and could only use a proxy, which was very frustrating. But I can't post or reply when using proxy. As a moderator, I feel deeply regretful. Today I used a proxy to go up and saw a few posts. I will answer them here by the way.

1. Let’s talk about URL mapping
Generally, there are two ways of url mapping. One is through mod_rewrite. There are many online teaching materials about this, so I won’t go into details. The other is to simulate in a program, such as /index.php/controller/action/var1/value1/var2/value2/ in zend Framework. In fact, the main method here is to use a unified input interface, then parse the URL, and finally forward it to the module in the corresponding controller.

I wrote two simple functions here to simulate.
The first function is mainly for address parsing, parsing out addresses similar to /index.php/controller/action/var1/value1/var2/value2/. Generally speaking, it needs to be parsed into three parts: controller, module, params.

Copy code The code is as follows:

/**
* Simple parsing of url routing, supporting /path/to/site/index.php/module/action/parm/value
* /path/to/site/index.php?/module/ Action/parm/value and
* /path/to/site/?/module/action/parm/value three forms of processing
*@param:null
*@return:router array
*/ 
function url_router(){
$path=strip_tags($_SERVER['REQUEST_URI']);
$strpos=strpos($path,'.php');
if($strpos){
$path=substr($path,$strpos+4); 
 }else{ 
  if(empty($_SERVER['QUERY_STRING'])){  
    $strpos=strpos($path,'? ');
if($strpos){
$path=substr($path,$strpos+1);
}else{
$path='';
            } 
                                                                                                                                                                                                                             $path=$_
if($path[0]=='/'){
$path=substr($path,1);
}
//Parse and route
if(! empty($path)){
$path=explode('/',$path);
$router['controller']=$path[0];
$router['action'] =(!empty($path[1]))?$path[1]:'index';
//print_r($path);
for($i=2;$i $params[$path[$i]]=(isset($path[$i+1]))?$path[$i+1]:' ';
}
$router['params']=$params;
}else{
//Default routing information
$router['controller']='index'; 🎜> $router['action']='index';
$router['params']=array();
}
return $router;
}
?>



The main url parsing function is completed here, and then the forwarding mapping is implemented. The following function is implemented (note that the implementation of this function is combined with my own architecture, so if you adopt it, you need to modify it accordingly. Of course, your If MVC is similar to zend Framework, it should not be much.)

Copy code The code is as follows:

function url_dispatch($router,$app_path='/app/controllers/')
{
require_once(SERVER_PATH.'/libs/controller.class.php');
$ controller=$router['controller'].'Controller';
//echo SERVER_PATH.$app_path.$controller.'.class.php';
if(!file_exists(SERVER_PATH.$app_path.$controller .'.class.php'))die('Missing necessary class!');
require_once(SERVER_PATH.$app_path.$controller.'.class.php');
$controller=new $controller ();
$controller->_setParam($router['params']);
$controller->{$router['action'].'Action'}();
return true;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317725.htmlTechArticleAfter the PHPCHINA server was relocated, I basically couldn’t access it and could only use a proxy, which was very frustrating. But I can't post or reply when using proxy. As a moderator, I feel deeply regretful. I used a proxy to go up today and saw...
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