首页 > 开发 > php > 正文

有关路由的实现

2017-09-06 13:38:06  来源:网友分享

在使用网站站点软件时,发现有的软件的根目录下的index.php可以生成多个不同的网页,如:

localhost/index.php/blog;
localhost/index.php/contact。

请问这种路由方式是如何实现的?请上示例代码,谢谢!

解决方案

对于服务器来说,www.xxx.com/index.php/test只能识别到www.xxx.com/index.php,这个是在Nginx或者Apche里面配置的,例如下面一段配置:

location ~ \.php$ {        fastcgi_split_path_info ^(.+\.php)(/.+)$;        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;        fastcgi_index index.php;        include fastcgi_params;        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        fastcgi_intercept_errors off;        fastcgi_buffer_size 16k;        fastcgi_buffers 4 16k;        fastcgi_connect_timeout 300;        fastcgi_send_timeout 300;        fastcgi_read_timeout 300;    }

至于后面的信息test,是PHP代码自己去识别的,其实就是一个参数,(至于PHP代码怎么接收到这个参数,你就得去看看Nginx或者Apache是怎么和PHP交互的了),当这个参数"test"拿到以后,你就可以根据这个参数做你想做的事,不同的参数,你就可以做不同的事,提供多样的功能,这也是我对路由的理解。