<?php
/**
*
* @author Orkhan Z. Maharramli (orkhan.maharramli@gmail.com)
*
*/
class Router
{
public $path;
public $config;
public $routes = array();
private static $instance;
public static function getInstance()
{
if (self::$instance === null)
{
self::$instance = new self();
}
return self::$instance;
}
public function __construct()
{
$this->config = Config::getInstance();
$this->routes = $this->_getRoutes();
$this->path = $this->_getPath();
header("HTTP/1.0 404 Not Found");
return;
}
public function __destruct()
{
$this->routes = array();
$this->config = null;
$this->path = null;
}
public function __get($name)
{
return $this->{$name};
}
/**
*
* Get routes from XML
*
* @return array Router Configs
*
*/
protected function _getRoutes()
{
if(count($this->config->routes->route) > 0)
{
foreach($this->config->routes->route as $routes)
{
$routes_array[(string)$routes->attributes()->pattern] = (string)$routes->attributes()->route;
}
}
else
{
$routes_array = array();
}
return $routes_array;
}
/**
*
* Get path from $_SERVER
*
* @return string URL
*
*/
protected function _getPath()
{
if(isset($_SERVER['PATH_INFO']))
{
$uri = $_SERVER['PATH_INFO'];
}
elseif(isset($_SERVER['REQUEST_URI']))
{
$uri = $_SERVER['REQUEST_URI'];
if(strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
{
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
}
elseif(strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
{
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
}
// This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
// URI is found, and also fixes the QUERY_STRING server var and $_GET array.
if(strncmp($uri, '?/', 2) === 0)
{
$uri = substr($uri, 2);
}
$parts = preg_split('#\?#i', $uri, 2);
$uri = $parts[0];
if(isset($parts[1]))
{
$_SERVER['QUERY_STRING'] = $parts[1];
parse_str($_SERVER['QUERY_STRING'], $_GET);
}
else
{
$_SERVER['QUERY_STRING'] = '';
$_GET = array();
}
$uri = parse_url($uri, PHP_URL_PATH);
}
else
{
$uri = false;
}
$uri = ltrim($uri, '/');
if(count($this->routes) > 0)
{
foreach($this->routes as $pattern => $route)
{
if(preg_match("~$pattern~", $uri))
{
$uri = preg_replace("~$pattern~", $route, $uri);
$uri = str_replace(array('//', '../'), '/', trim($uri, '/'));
$uri = explode('/', $uri);
}
}
}
else
{
$uri = str_replace(array('//', '../'), '/', trim($uri, '/'));
$uri = explode('/', $uri);
}
return $uri;
}
}
router-class-2011-11-27.zip_Class 11
版权申诉
31 浏览量
2022-09-24
03:58:27
上传
评论
收藏 2KB ZIP 举报
weixin_42651887
- 粉丝: 79
- 资源: 1万+
最新资源
- MI4407-VB一款SOP8封装P-Channel场效应MOS管
- java实验2.doc
- opencv-基于c++实现的opencv图像处理算法之区域生长分割算法.zip
- MI3475-VB一款SOT23封装P-Channel场效应MOS管
- Fortran语言教程&案例&相关项目资源
- Labview 互斥按钮
- java小说人物分析.doc
- opencv-基于c++实现的opencv图像处理算法之双线性插值算法.zip
- 《STM32单片机+DHT11温湿度+MQ甲醛浓度+BH1750光照强度+按键+OLED屏幕+蜂鸣器+继电器》源代码
- python爬虫基础知识原理及Scrapy框架以及scrapy-redis分布式策略入门知识.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈