File size: 1,730 Bytes
a84929c
 
 
 
 
 
 
789f143
 
a84929c
 
 
f23d41b
 
 
a84929c
 
f23d41b
 
 
 
9a18c8c
 
21dd205
 
 
 
 
023cdb0
9a18c8c
a96c1e5
 
023cdb0
 
 
 
 
 
 
 
 
 
 
a84929c
 
 
f2b19e5
 
 
 
 
 
 
 
a84929c
 
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
<?php
    /*
    Map URL from browser as the following syntax

    fqdn[/path]/controller/view/[params/]

    example:
    example.com/welcome/index
    example.com/welcome/test/4
    */

    class Core{
        protected $defaultController;
        protected $defaultMethod;
        protected $defaultParams;

        public function __construct(){
            $this->defaultController = ucwords('welcome');
            $this->defaultMethod = 'index';
            $this->defaultParams = [];
            
            // print_r($this->getURL());
            $url = $this->getURL();
            if(isset($url[0])){
                if (file_exists('../app/controllers/' . ucwords($url[0]) . '.php')) {
                    $this->defaultController = ucwords($url[0]);
                    unset($url[0]);
                }

            }
            require_once('../app/controllers/' . $this->defaultController . '.php');
            $this->defaultController = new $this->defaultController;

            if (isset($url[1])) {
                if (method_exists($this->defaultController, $url[1])) {
                    $this->defaultMethod = $url[1];
                    unset($url[1]);
                }
            }
            
            $this->defaultParams = $url ? array_values($url) : [];

            call_user_func_array([$this->defaultController,$this->defaultMethod],$this->defaultParams);
        }

        public function getURL(){
            // echo $_GET['url'];

            if (isset($_GET['url'])) {
                $url = rtrim($_GET['url'], '/');
                $url = filter_var($url,FILTER_SANITIZE_URL);
                $url = explode('/', $url);
                return $url;
            }
        }
    }