Class Code:
Copy the Code code as follows:
Class Router
{
Public Function Getrouter ($types = 1)
{
if (Isset ($_server[' path_info '))
{
$query _string = substr (Array ('. html ', '. htm ', '. asp ', '//'), ' ', ' $_server[' path_info ']), 1);
}
Else
{
$query _string = str_replace ($_server[' script_name '), ' ', $_server[' php_self ']);
}
if ($types = = 1)
{
The first type is separated by/
$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 ';
}
Remove empty items
foreach ($temp as $val)
{
if ($val)
{
$url [] = $val;
}
}
List ($controller, $operate) = $url;
The case with parameters
$params = Array ();
if (count ($url) >2)
{
Array_shift ($url);
Array_shift ($url);
$params = $url;
}
Return
Array
"Controller" = $controller,
"Operate" = $operate,
"Params" = $params,
);
}
}
?>
Call Method:
Copy the Code code as follows:
$url = new Router ();
$url->getrouter (1);
Print_r ($url); Here you can see the elements
?>
The above introduction to the first ancient Olympic Games in the country has several PHP to write a simple routing class, including the first Olympic Games in the country has several aspects of the content, I hope that the PHP tutorial interested in a friend helpful.