d
This commit is contained in:
78
src/Hura/Router.php
Normal file
78
src/Hura/Router.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Hura;
|
||||
|
||||
|
||||
class Router {
|
||||
|
||||
private array $path_config = [];
|
||||
|
||||
public function __construct() {
|
||||
$path_config_file = CONFIG_DIR . '/routing.php';
|
||||
$this->path_config = require $path_config_file;
|
||||
}
|
||||
|
||||
// url: asdas.php?para1=value1
|
||||
public function getRouting(): array
|
||||
{
|
||||
$parsed = Url::parse($_SERVER['REQUEST_URI']); //abc/product?param1=12¶m2=value2
|
||||
//print_r($parsed);
|
||||
|
||||
// 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'],
|
||||
'url' => $_SERVER['REQUEST_URI'],
|
||||
];
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
// check database
|
||||
|
||||
// else error
|
||||
return [
|
||||
'module' => "error" ,
|
||||
'view' => "error" ,
|
||||
'view_id' => "not_found",
|
||||
'query' => $parsed['query'],
|
||||
'url' => $_SERVER['REQUEST_URI'],
|
||||
];
|
||||
|
||||
|
||||
// auto parse path base on convention: admin/module/view/view_id
|
||||
/* $ele = explode("/", $parsed['path']);
|
||||
|
||||
$module = $ele[2] ?? 'home';
|
||||
$view = isset($ele[3]) ? $ele[3] : getRequest('view', 'home');
|
||||
$view_id = isset($ele[4]) ? $ele[4] : getRequest('id', 'view_id');
|
||||
|
||||
// else error
|
||||
return [
|
||||
'module' => preg_replace("/[^a-z0-9_\-]/i","", $module ) ,
|
||||
'view' => preg_replace("/[^a-z0-9_\-]/i","", $view ) ,
|
||||
'view_id' => preg_replace("/[^a-z0-9_]/i","", $view_id ),
|
||||
'query' => $parsed['query'],
|
||||
'url' => $_SERVER['REQUEST_URI'],
|
||||
];*/
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user