Files
admin_hura_8/inc/Hura8/Router.php

68 lines
2.2 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
2024-01-31 09:34:00 +07:00
// print_r($parsed);
2024-01-28 11:27:52 +07:00
// 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')),
2024-01-31 09:34:00 +07:00
'view_id'=> 0,
2024-01-28 11:27:52 +07:00
'query' => $parsed['query'],
2024-01-31 09:34:00 +07:00
'url' => $parsed['path'],
2024-01-28 11:27:52 +07:00
];
}
// check match pattern in $this->path_config
2024-01-29 11:46:07 +07:00
/*foreach ($this->path_config as $_config => $_route ) {
2024-01-28 11:27:52 +07:00
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);
}
2024-01-29 11:46:07 +07:00
}*/
2024-01-28 11:27:52 +07:00
// auto parse path base on convention: admin/module/view/view_id
$ele = explode("/", $parsed['path']);
2024-01-29 11:46:07 +07:00
$module = $ele[2] ?? 'home';
$view = isset($ele[3]) ? $ele[3] : getRequest('view', 'home');
$view_id = isset($ele[4]) ? $ele[4] : getRequest('id', 'view_id');
2024-01-28 11:27:52 +07:00
// else error
return [
2024-01-29 11:46:07 +07:00
'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 ),
2024-01-28 11:27:52 +07:00
'query' => $parsed['query'],
2024-01-31 09:34:00 +07:00
'url' => $parsed['path'],
2024-01-28 11:27:52 +07:00
];
}
}