Размер файла: 6.6Kb
<?
class Route {
public $action = NULL;
public $module_name = NULL;
public $module_path = NULL;
public $controller_exists = TRUE;
public $module_method;
/**
* Сегменты
*/
public $segment1;
public $segment2;
public $segment3;
public function __construct() {
$this->parse_query();
$this->route();
}
public function parse_query(){
$request_string = $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING'];
if (preg_match('#\/[a-z0-9\.\/?\&\=]{1,25}#i', $request_string)) {
$ex_string = explode('/', $request_string);
}
$query = str_replace(URL, '', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
$ex = explode('?', $query);
$query = $ex[0];
$query = str_replace(FILE_EX, '', $query);
# Правила роутинга для каждого модуля
//$this->module_name = $ex_string[1];
$rules = '';
if (file_exists(DATADIR . 'route/' . $ex_string[1] . '.dat')) {
$rules .= implode('', file(DATADIR . 'route/' . $ex_string[1] . '.dat')) . PHP_EOL;
}
$rules .= '([a-z0-9_\-]*)([\.a-z0-9]*)#segment1=$1' . PHP_EOL;
$rules .= '([a-z0-9_\-]*)/([a-z0-9_\-]*)([\.a-z0-9]*)#segment1=$1&segment2=$2' . PHP_EOL;
$rules .= '([a-z0-9_\-]*)/([a-z0-9_\-]*)/([a-z0-9_\-]*)([\.a-z0-9]*)#segment1=$1&segment2=$2&segment3=$3' . PHP_EOL;
$rules = explode("\n", $rules);
foreach ($rules as $rule) {
$rule = trim($rule);
if (strpos($rule, '#') == 0 or $rule == '')
continue;
$ex = explode('#', $rule);
if (preg_match('~^' . $ex[0] . '$~', $query)) {
$result = preg_replace('~^' . $ex[0] . '$~', $ex[1], $query, 1);
$vars = explode('&', $result);
foreach ($vars as $var) {
$_ex = explode('=', $var);
$_GET[$_ex[0]] = $_ex[1];
}
break;
}
}
# Отдельное правило для просмотра файла по id
if (preg_match('#/([0-9]{1,11})$#', $request_string, $idfile)){
$_GET['segment1'] = 'index_page';
$_GET['segment2'] = 'view';
$_GET['idfile'] = $idfile[1];
}
}
/**
* Функция определяет контроллер и action
*/
public function route() {
# Определение сегментов
if($this->_check_segments()) {
# Если нет сегментов, подключаем модуль по умолчанию
if(empty($this->segment1)) {
if(file_exists(ROOT .'modules/'. MODULE_DEFAULT .'/system/'. MODULE_DEFAULT .'.php')) {
$this->module_path = 'modules/'. MODULE_DEFAULT .'/system/'. MODULE_DEFAULT .'.php';
$this->module_method = MODULE_DEFAULT;
$this->action = '';
$this->module_name = MODULE_DEFAULT;
}
else $this->controller_exists = FALSE;
}
# Если указаны все 3 сегмента
if(!empty($this->segment1) && !empty($this->segment2) && !empty($this->segment3)) {
if(file_exists(ROOT .'modules/'. $this->segment1 .'/system/'. $this->segment1 .'_'. $this->segment2 .'.php')) {
$this->module_path = 'modules/'. $this->segment1 .'/system/'. $this->segment1 .'_'. $this->segment2 .'.php';
$this->module_method = $this->segment1 .'_'. $this->segment2;
$this->action = $this->segment3;
$this->module_name = $this->segment1;
}
else $this->controller_exists = FALSE;
}
# Если указаны 2 сегмента
elseif(!empty($this->segment1) && !empty($this->segment2)) {
if(file_exists(ROOT .'modules/'. $this->segment1 .'/system/'. $this->segment1 .'_'. $this->segment2 .'.php')) {
$this->module_path = 'modules/'. $this->segment1 .'/system/'. $this->segment1 .'_'. $this->segment2 .'.php';
$this->module_method = $this->segment1 .'_'. $this->segment2;
$this->action = '';
$this->module_name = $this->segment1;
}
elseif(file_exists(ROOT .'modules/'. $this->segment1 .'/system/'. $this->segment1 .'.php')) {
$this->module_path = 'modules/'. $this->segment1 .'/system/'. $this->segment1 .'.php';
$this->module_method = $this->segment1;
$this->action = $this->segment2;
$this->module_name = $this->segment1;
}
else $this->controller_exists = FALSE;
}
# Если указан только 1 сегмент
elseif(!empty($this->segment1)) {
if(file_exists(ROOT . 'modules/'. $this->segment1 .'/system/'. $this->segment1 .'.php')) {
$this->module_path = 'modules/'. $this->segment1 .'/system/'. $this->segment1 .'.php';
$this->module_method = $this->segment1;
$this->action = '';
$this->module_name = $this->segment1;
}
else $this->controller_exists = FALSE;
}
if($this->controller_exists) {
define('ROUTE_MODULE_PATH', $this->module_path);
define('ROUTE_MODULE_NAME', $this->module_name);
define('ROUTE_MODULE_ACTION', $this->action);
define('ROUTE_MODULE_METHOD', $this->module_method);
}
else header('Location: '. p_url('main/page_not_found', '', true));
}
}
/**
* Проверка правильности сегментов
*/
protected function _check_segments() {
$check_segments = true;
if(!empty($_GET['segment1'])) {
if(preg_match('~^[0-9a-z_-]*$~', $_GET['segment1'])) $this->segment1 = $_GET['segment1'];
else $check_segments = false;
}
if(!empty($_GET['segment2'])) {
if(preg_match('~^[0-9a-z_-]*$~', $_GET['segment2'])) $this->segment2 = $_GET['segment2'];
else $check_segments = false;
}
if(!empty($_GET['segment3'])) {
if(preg_match('~^[0-9a-z_-]*$~', $_GET['segment3'])) $this->segment3 = $_GET['segment3'];
else $check_segments = false;
}
if(!$check_segments) display_error('Ошибка регистрации сегментов!');
return true;
}
}
?>