Answers to several questions on Phpchina: URL mapping _php Tips

Source: Internet
Author: User
Tags zend zend framework
Phpchina server Relocation, I will basically not go, can only use agent, depressed. But with agent incredibly can't post, replies. As a moderator, deeply regret, today with the agent went to see a few posts, by the way here to answer.

1, we say URL mapping bar
There are two ways of general URL mapping, one is through the mod_rewrite implementation, this kind of online textbooks a lot I don't say more. The other is to simulate in the program, such as in the Zend framework of the way/index.php/controller/action/var1/value1/var2/value2/. In fact, the most important way is through a unified input interface, then parse the URL, and finally forward to the corresponding controller module.

I've written two simple functions here to simulate.
The first function is to resolve the address, the address of a similar/index.php/controller/action/var1/value1/var2/value2/, in general, to parse into three parts: Controller,module, Params.
Copy Code code as follows:

?
/**
* Simple parsing of URL routing, support for/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 types of treatment
* @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 = ';
}
}else{
$path =$_server[' query_string '];
}
}
Unify the $path format, if $path the first word identifier/then remove
if ($path [0]== '/') {
$path =substr ($path, 1);
}
parsing, and routing
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 <sizeof ($path); $i = $i +2) {
$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;
}
?>


This completes the main URL parsing function, then the forwarding map, the following function implementation (note that the implementation of this function is a combination of my own architecture, so you need to use the words of the corresponding changes, of course, your MVC if similar to the Zend Framework, it should not be much. )

Copy Code code 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 the necessary class! ');  
   require_once (server_path. $app _path. $controller. '). class.php ');  
    $controller =new  $controller ();  
     $controller->_setparam ($router [' params ']);  
    $controller->{$router [ ' Action ']. ' Action '} ();  
   return true;  
}  
?>   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.