Answer several questions in PHPCHINA: URL ing

Source: Internet
Author: User
Tags zend framework

After the PHPCHINA server was relocated, I basically couldn't go up. I had to use a proxy, so I was depressed. However, you cannot post or reply to the post with a proxy. As the moderator, I am deeply sorry. I have seen several posts on behalf of you today. By the way, I will reply here.

1. Let's talk about URL ing.
Generally, there are two methods for url ing. One is implemented through mod_rewrite. I will not talk about this online course material much more. Another method is to simulate it in a program, such as the method in zend Framework,/index. php/controller/action/var1/value1/var2/value2 /. The method here is mainly to use a unified input interface, parse the url, and finally forward it to the module in the corresponding controller.

I have written two simple functions here for simulation.
The first function is mainly to parse the address, and put something similar to/index. the address of php/controller/action/var1/value1/var2/value2/is parsed into three parts: controller, module, and params.Copy codeThe Code is as follows: <?
/**
* Simple parsing of url routes, 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 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 = '';
}
} Else {
$ Path = $ _ SERVER ['query _ string'];
}
}
// Unify the $ path format. If the first character of $ path is/, remove it.
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 <sizeof ($ path); $ I = $ I + 2 ){
$ Params [$ path [$ I] = (isset ($ path [$ I + 1])? $ Path [$ I + 1]: '';
}
$ Router ['params'] = $ params;
} Else {
// Default route information
$ Router ['controller'] = 'index ';
$ Router ['action'] = 'index ';
$ Router ['params'] = array ();
}
Return $ router;
}
?>

Here we will complete the main url parsing function, and then forward ing. The following function implementation (note that the implementation of this function is based on my own architecture, so you need to modify it accordingly. Of course, if your MVC is similar to the zend Framework, you should not need 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 (' the necessary class is missing! ');
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.