Files
admin_hura_8/inc/Hura8/Router.php

61 lines
1.9 KiB
PHP
Raw Normal View History

2024-01-28 11:27:52 +07:00
<?php
namespace Hura8;
class Router {
private $path_config = [];
public function __construct() {
//if( ! $this->path_config) {
//$path_config_file = APP_DIR . '/config/routing.php';
//$this->path_config = require $path_config_file;
//}
}
// url: admin/abc/product.php?para1=value1
public function getRouting() {
$parsed = Url::parse($_SERVER['REQUEST_URI']); //abc/product?param1=12&param2=value2
// home
if($parsed['path'] == '/') {
return [
'module' => preg_replace("/[^a-z0-9_\-]/i","", getRequest('module', 'home')),
'view' => preg_replace("/[^a-z0-9_\-]/i","", getRequest('view', 'home')),
'view_id' => 0,
'query' => $parsed['query'],
];
}
// check match pattern in $this->path_config
foreach ($this->path_config as $_config => $_route ) {
if(preg_match("{^".$_config."$}", $parsed['path'], $match )) {
if(isset($_route['query']) && is_array($_route['query'])) {
$_route['query'] = array_merge($_route['query'], $parsed['query']);
}else{
$_route['query'] = $parsed['query'];
}
return array_merge([
'path' => $parsed['path'],
'match' => $match,
], $_route);
}
}
// auto parse path base on convention: admin/module/view/view_id
$ele = explode("/", $parsed['path']);
// else error
return [
'module' => $ele[2] ?? 'home',
'view' => isset($ele[3]) ? preg_replace("/[^a-z0-9_\-]/i","", $ele[3] ) : 'home',
'view_id' => isset($ele[4]) ? preg_replace("/[^a-z0-9_]/i","", $ele[4] ) : '',
'query' => $parsed['query'],
];
}
}