Write a simple routing class in php_PHP tutorial

WBOY
Release: 2016-07-21 15:30:55
Original
947 people have browsed it

Class code:

Copy code The code is as follows:

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 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;
//With parameters
$params = array();
if ( count($url)>2 )
{
array_shift($url);
array_shift ($url);
$params = $url;
}
return
array(
"Controller" => $controller,
"Operate" => $operate ,
"params" => $params,
);
}
}
?>

Calling method:
Copy code The code is as follows:

$url = new Router();
$url->getRouter (1);
print_r($url); //You can see each element here
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323174.htmlTechArticleClass code: Copy the code as follows: ?php class Router { public function getRouter($types = 1) { if ( isset($_SERVER['PATH_INFO']) ) { $query_string = substr(str_replace(array('....
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!