Answers to several questions on Phpchina: URL mapping _php Tutorial

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

1, we say the URL map it
There are two ways of general URL mapping, one is realized through mod_rewrite, this kind of online teaching material I do not say much. Another is the simulation in the program, such as the Zend framework in the same way/index.php/controller/action/var1/value1/var2/value2/. In fact, the main way is through a unified input interface, and then parse the URL, and finally forwarded to the corresponding controller module.

I've written here two simple functions to simulate.
The first function is mainly for address resolution, the similar/index.php/controller/action/var1/value1/var2/value2/address resolution, generally to parse into three parts: Controller,module, Params.
Copy CodeThe code is 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 format of the $path if the first word of $path identifier/is removed
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 $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, and then the forwarding map, the following function implementation (Note that this function is implemented in conjunction with my own architecture, so you need to modify the words, of course, if your MVC is similar to the Zend Framework, that should not be much.) )

Copy CodeThe 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 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;
}
?>

http://www.bkjia.com/PHPjc/317725.html www.bkjia.com true http://www.bkjia.com/PHPjc/317725.html techarticle Phpchina Server Relocation, I will basically not go, only with agents, depressed. But with agents incredibly can not post, replies. As a moderator, deeply regret, today with agents to see ...

  • 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.