-
Notifications
You must be signed in to change notification settings - Fork 10
/
App.php
58 lines (43 loc) · 1.61 KB
/
App.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
<?php
class App{
/*
* definig defualt parameters if not specified by the user
*/
protected $controller = 'home';
protected $method = 'index';
protected $language = 'en';
protected $params = [];
public function __construct(){
/*
* GET request should be in the following form : URL/language/controller/method/parameters
*/
//getting the request parameters
$req = $this->parseUrl();
//setting the langauge to default language if not specified.
if($req != NULL && file_exists('../app/languages/' . $req[0])){
define('LANGUAGE', array_shift($req));
}else{
define('LANGUAGE', $this->language);
}
if(file_exists('../app/controllers/' . $req[0] . '.php')){
$this->controller = $req[0];
unset($req[0]);
}
require_once '../app/controllers/' . $this->controller . '.php';
$this->controller = new $this->controller;
if(isset($req[1])){
if(method_exists($this->controller,$req[1])){
$this->method = $req[1];
unset($req[1]);
}
}
$this->params = $req ? array_values($req): [];
//sending the req array as parameters for the method function f(req[0],req[1] ..etc){}
call_user_func_array([$this->controller,$this->method], $this->params);
}
public function parseUrl(){
if(isset($_GET['req'])){
return $req = explode('/',filter_var(rtrim($_GET['req'],'/'),FILTER_SANITIZE_URL));
}
}
}