Class Code:
Copy codeThe Code is as follows:
<? 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)
{
// The first type is separated by a slash (/).
$ 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 null items
Foreach ($ temp as $ val)
{
If ($ val)
{
$ Url [] = $ val;
}
}
List ($ controller, $ operate) = $ url;
// When parameters exist
$ 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 codeThe Code is as follows:
<? Php
$ Url = new Router ();
$ Url-> getRouter (1 );
Print_r ($ url); // you can see each element here
?>