-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
69 lines (59 loc) · 2.01 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
function parseURL(string $url){
$gosho = substr($url,0,strlen(APP_ROOT ."/"));
if($gosho != APP_ROOT . "/") {
die('APP_ROOT IS NOT CORRECT, SHOULD BE blog');
}
$url = substr($url,strlen(APP_ROOT . "/"));
$urlParts = array_values(array_filter(explode('/',$url)));
$controllerName = DEFAULT_CONTROLLER;
$actionName = DEFAULT_ACTION;
$params = [];
//if we have atleast 1 that must be controller name
if(count($urlParts)>0){
$controllerName = $urlParts[0];
//if we have more than 1 next one must be action
if(count($urlParts)>1){
$actionName = $urlParts[1];
//if we have more than 2 next ones must be params
if(count($urlParts)>2){
//parts after url must be params so we slice with offset of 2
$params = array_slice($urlParts,2);
}
}
}
if(class_exists($controllerName . "controller")){
$controllerClassName = $controllerName . "controller";
$controller = new $controllerClassName($controllerName,$actionName);
}
else {
require_once "Views/_layout/page-not-found.php";
die();
}
if(method_exists($controller,$actionName) && is_callable(array($controller,$actionName))){
$controller->$actionName($params);
if($actionName =="deleteComment"){
$controller->isPost = true;
}
if($controllerName=="votes"){
$controller->renderView(null,false,false);
return;
}
$controller->renderView(null);
}
else{
require_once "Views/_layout/page-not-found.php";
die();
}
}
function __autoload(string $className){
$className = $className . ".php";
$controllersPrefix = "controllers/";
$modelsPrefix = "Models/";
if(file_exists($controllersPrefix . $className)){
include ($controllersPrefix . $className);
}
else if(file_exists($modelsPrefix . $className)){
include($modelsPrefix . $className);
}
}