php编写一个简单的路由类
前端之家收集整理的这篇文章主要介绍了
php编写一个简单的路由类,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
类代码:
<div class="codetitle"><a style="CURSOR: pointer" data="26214" class="copybut" id="copybut26214" onclick="doCopy('code26214')"> 代码如下:
<div class="codebody" id="code26214">
<?
PHP class Router
{
public function getRouter($types = 1)
{
if ( isset($_SERVER['PATH_INFO']) )
{
$query_string = substr(str_replace(array('.html','.htm','.asp','//'),'',$_SERVER['PATH_INFO']),1);
}
else
{
$query_string = str_replace($_SERVER['SCRIPT_NAME'],$_SERVER['
PHP_SELF']);
}
if ( $types == 1 )
{
// 第一种类型以/分隔
$temp = explode('/',$query_string);
}
elseif ($types == 2)
{
$temp = explode('-',$query_string);
}
elseif ($types == 3)
{
return array('Controller'=>$_GET['controller']);
}
if ( empty($temp[0]) )
{
return array('Controller' => 'index','Operate' => 'index');
}
if ( empty($temp[1]) )
{
$temp[] = 'index';
}
//
去除空项
foreach ($temp as $val)
{
if ($val)
{
$url[] = $val;
}
}
list($controller,$operate) = $url;
//有参数的情况
$params = array();
if ( count($url)>2 )
{
array_shift($url);
array_shift($url);
$params = $url;
}
return
array(
"Controller" => $controller,
"Operate" => $operate,
"params" => $params,
);
}
}
?>