Making functions for the principal routines
This commit is contained in:
parent
754bdc9c18
commit
43b8604d97
2 changed files with 112 additions and 95 deletions
99
index.php
99
index.php
|
|
@ -3,109 +3,20 @@
|
||||||
use PhangoApp\PhaUtils\Utils;
|
use PhangoApp\PhaUtils\Utils;
|
||||||
|
|
||||||
include('libraries/Utils.php');
|
include('libraries/Utils.php');
|
||||||
include('vendor/autoload.php');
|
include('libraries/Routes.php');
|
||||||
|
|
||||||
class Config {
|
if(is_file('vendor/autoload.php')) {
|
||||||
|
|
||||||
static public $home_module='welcome';
|
include('vendor/autoload.php');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
|
||||||
$path_info=isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
|
|
||||||
|
|
||||||
// Very simple router, two levels deep, module and controller
|
|
||||||
// First deep element in path modules/{module}/controllers/index.php -> /module
|
|
||||||
// Second deep element in path modules/{module}/controllers/{controller}.php -> /module/controller
|
|
||||||
// Third deep element and more in path are variables into controller function
|
|
||||||
// Filter the elements path with slugify function.
|
|
||||||
|
|
||||||
Utils::load_config('settings/config');
|
Utils::load_config('settings/config');
|
||||||
|
|
||||||
if($path_info!='') {
|
$path_info=isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
|
||||||
|
|
||||||
$arr_path=explode('/', $path_info);
|
echo \PhangoApp\PhaRouter\get_route($path_info);
|
||||||
|
|
||||||
if($arr_path[count($arr_path)-1]=='') {
|
|
||||||
|
|
||||||
unset($arr_path[count($arr_path)-1]);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
unset($arr_path[0]);
|
|
||||||
|
|
||||||
foreach($arr_path as $k => $info) {
|
|
||||||
|
|
||||||
$arr_path[$k]=Utils::slugify($info);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (count($arr_path)) {
|
|
||||||
|
|
||||||
case 0:
|
|
||||||
|
|
||||||
$controller='modules/'.Config::$home_module.'/controllers/index.php';
|
|
||||||
|
|
||||||
get_controller($controller, []);
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
|
|
||||||
$controller='modules/'.$arr_path[1].'/controllers/index.php';
|
|
||||||
|
|
||||||
get_controller($controller, []);
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
|
|
||||||
$controller='modules/'.$arr_path[1].'/controllers/'.$arr_path[2].'.php';
|
|
||||||
|
|
||||||
get_controller($controller, []);
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
|
|
||||||
$controller='modules/'.$arr_path[1].'/controllers/'.$arr_path[2].'.php';
|
|
||||||
|
|
||||||
$args=array_slice($arr_path, 2, count($arr_path));
|
|
||||||
|
|
||||||
get_controller($controller, $args);
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
|
|
||||||
$controller='modules/'.Config::$home_module.'/controllers/index.php';
|
|
||||||
|
|
||||||
get_controller($controller, []);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
ob_end_flush();
|
ob_end_flush();
|
||||||
|
|
||||||
function get_controller($controller, $args=[]) {
|
|
||||||
|
|
||||||
if(is_file($controller)) {
|
|
||||||
|
|
||||||
include($controller);
|
|
||||||
|
|
||||||
echo call_user_func_array('controller', $args);
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
|
|
||||||
echo 'Page not found';
|
|
||||||
|
|
||||||
http_response_code(404);
|
|
||||||
|
|
||||||
exit(1);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
||||||
106
libraries/Routes.php
Normal file
106
libraries/Routes.php
Normal file
|
|
@ -0,0 +1,106 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhangoApp\PhaRouter;
|
||||||
|
use PhangoApp\PhaUtils\Utils;
|
||||||
|
|
||||||
|
class Config {
|
||||||
|
|
||||||
|
static public $home_module='welcome';
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Very simple router, two levels deep, module and controller
|
||||||
|
// First deep element in path modules/{module}/controllers/index.php -> /module
|
||||||
|
// Second deep element in path modules/{module}/controllers/{controller}.php -> /module/controller
|
||||||
|
// Third deep element and more in path are variables into controller function
|
||||||
|
// Filter the elements path with slugify function.
|
||||||
|
|
||||||
|
function get_controller($controller, $args=[]) {
|
||||||
|
|
||||||
|
if(is_file($controller)) {
|
||||||
|
|
||||||
|
include($controller);
|
||||||
|
|
||||||
|
return call_user_func_array('controller', $args);
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
echo 'Page not found';
|
||||||
|
|
||||||
|
http_response_code(404);
|
||||||
|
|
||||||
|
exit(1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_route($path_info) {
|
||||||
|
|
||||||
|
if($path_info!='') {
|
||||||
|
|
||||||
|
$arr_path=explode('/', $path_info);
|
||||||
|
|
||||||
|
if($arr_path[count($arr_path)-1]=='') {
|
||||||
|
|
||||||
|
unset($arr_path[count($arr_path)-1]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
unset($arr_path[0]);
|
||||||
|
|
||||||
|
foreach($arr_path as $k => $info) {
|
||||||
|
|
||||||
|
$arr_path[$k]=Utils::slugify($info);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (count($arr_path)) {
|
||||||
|
|
||||||
|
case 0:
|
||||||
|
|
||||||
|
$controller='modules/'.\PhangoApp\PhaRouter\Config::$home_module.'/controllers/index.php';
|
||||||
|
|
||||||
|
return \PhangoApp\PhaRouter\get_controller($controller, []);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
|
||||||
|
$controller='modules/'.$arr_path[1].'/controllers/index.php';
|
||||||
|
|
||||||
|
return \PhangoApp\PhaRouter\get_controller($controller, []);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
|
||||||
|
$controller='modules/'.$arr_path[1].'/controllers/'.$arr_path[2].'.php';
|
||||||
|
|
||||||
|
return \PhangoApp\PhaRouter\get_controller($controller, []);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
|
||||||
|
$controller='modules/'.$arr_path[1].'/controllers/'.$arr_path[2].'.php';
|
||||||
|
|
||||||
|
$args=array_slice($arr_path, 2, count($arr_path));
|
||||||
|
|
||||||
|
return \PhangoApp\PhaRouter\get_controller($controller, $args);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
$controller='modules/'.\PhangoApp\PhaRouter\Config::$home_module.'/controllers/index.php';
|
||||||
|
|
||||||
|
return \PhangoApp\PhaRouter\get_controller($controller, []);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue