本节内容:
php路由
在php中实现简单的路由功能,需要用到二个方法:
$_SERVER['PATH_INFO'] 建立对应关系
$_SERVER['QUERY_STRING'] 获取URI?后面的参数
例子,index.php
复制代码 代码示例:
$str=explode("/", $_SERVER['PATH_INFO']); // /classname/method
if(isset($str[1])){
require_once 'controllers.php';
$test=new test(); //加载类
if(isset($str[2])) //记载方法
{
$test->$str[2]();
}else{
$test->index();
}
}
2,控制器controllers.php代码
复制代码 代码示例:
class test
{
public function __construct()
{
}
public function index()
{
echo '这是默认的方法';
}
public function test()
{
echo '这是test()方法';
}
public function __call($name,$ar)
{ // www.jbxue.com
//echo $name."
";
//echo $ar."
";
echo '不要乱调用方法';
}
}
访问:
http://localhost/index.php/class/test
http://localhost/index.php/class/test[]