/+junk/StupidMVC

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/StupidMVC
1 by Gustav Hartvigsson
* initial code
1
<?php
2
3 by Gustav Hatvigsson
* Made the router a class
3
class Router {
7 by Gustav Hartvigsson
* a few more functions...
4
3 by Gustav Hatvigsson
* Made the router a class
5
  private $list_controllers = [];
7 by Gustav Hartvigsson
* a few more functions...
6
3 by Gustav Hatvigsson
* Made the router a class
7
  private $default_controller = null;
7 by Gustav Hartvigsson
* a few more functions...
8
4 by Gustav Hatvigsson
* Added FourOhFour.php
9
  private $four_oh_four_controller = null;
7 by Gustav Hartvigsson
* a few more functions...
10
4 by Gustav Hatvigsson
* Added FourOhFour.php
11
  public function
12
  register_controller ($controller_name) {
3 by Gustav Hatvigsson
* Made the router a class
13
    if (isset ($list_controllers[$controller_name])) {
14
      return;
15
    }
7 by Gustav Hartvigsson
* a few more functions...
16
3 by Gustav Hatvigsson
* Made the router a class
17
    require (APP_DIR . "Controllers" . DS . $controller_name  . ".php");
7 by Gustav Hartvigsson
* a few more functions...
18
3 by Gustav Hatvigsson
* Made the router a class
19
    $this->list_controllers[$controller_name] = $controller_name;
7 by Gustav Hartvigsson
* a few more functions...
20
3 by Gustav Hatvigsson
* Made the router a class
21
  }
7 by Gustav Hartvigsson
* a few more functions...
22
4 by Gustav Hatvigsson
* Added FourOhFour.php
23
  public function
24
  register_404_controller ($controller_name) {
25
    if (isset($this->four_oh_four_controller)){
26
      return;
27
    }
28
    $this->four_oh_four_controller = $controller_name;
29
    $this->register_controller ($controller_name);
30
  }
7 by Gustav Hartvigsson
* a few more functions...
31
3 by Gustav Hatvigsson
* Made the router a class
32
  public function register_default_controller ($controller_name) {
33
    if (isset($this->default_controller)) {
34
      return;
35
    }
36
    $this->default_controller = $controller_name;
37
    $this->register_controller ($controller_name);
38
  }
7 by Gustav Hartvigsson
* a few more functions...
39
40
  public function
41
  route () {
42
43
3 by Gustav Hatvigsson
* Made the router a class
44
    var_dump ($this->list_controllers);
7 by Gustav Hartvigsson
* a few more functions...
45
3 by Gustav Hatvigsson
* Made the router a class
46
    $url = "";
47
    $method = "default_method";
48
    $ctrl = $this->default_controller;
49
    $path = "";
7 by Gustav Hartvigsson
* a few more functions...
50
3 by Gustav Hatvigsson
* Made the router a class
51
    $request_url = '';
52
    if (isset($_SERVER['REQUEST_URI'])) {
53
      $request_url = $_SERVER['REQUEST_URI'];
54
    }
55
    //echo "Request URL: " . $request_url . "\n";
56
    $script_url = '';
57
    if (isset($_SERVER['PHP_SELF'])){
58
      $script_url = $_SERVER['PHP_SELF'];
59
    }
60
    //echo "Script URL: " . $script_url . "\n";
7 by Gustav Hartvigsson
* a few more functions...
61
3 by Gustav Hatvigsson
* Made the router a class
62
    if($request_url != $script_url) {
4 by Gustav Hatvigsson
* Added FourOhFour.php
63
      $url = trim(preg_replace('/'.
64
        str_replace('/', '\/',
65
          str_replace('index.php', '', $script_url))
66
            .'/', '', $request_url, 1), '/');
3 by Gustav Hatvigsson
* Made the router a class
67
      //echo "url: " . $url . "\n";
68
    }
7 by Gustav Hartvigsson
* a few more functions...
69
3 by Gustav Hatvigsson
* Made the router a class
70
    $parts = explode ('/', $url );
7 by Gustav Hartvigsson
* a few more functions...
71
3 by Gustav Hatvigsson
* Made the router a class
72
    var_dump ($parts);
7 by Gustav Hartvigsson
* a few more functions...
73
3 by Gustav Hatvigsson
* Made the router a class
74
    if (isset ($parts[0]) && $parts[0] != '') {
75
      $ctrl = $parts[0];
76
    }
7 by Gustav Hartvigsson
* a few more functions...
77
3 by Gustav Hatvigsson
* Made the router a class
78
    if (isset ($parts[1]) && $parts[1] != '') {
79
      $method = $parts[1];
80
    }
7 by Gustav Hartvigsson
* a few more functions...
81
3 by Gustav Hatvigsson
* Made the router a class
82
    var_dump ($ctrl);
83
    var_dump ($method);
7 by Gustav Hartvigsson
* a few more functions...
84
3 by Gustav Hatvigsson
* Made the router a class
85
    if(isset ($this->list_controllers[$ctrl])) {
86
      $controller_obj = new $ctrl ();
87
      $ctrl_method_list = $controller_obj->get_methods();
88
      if ($method != "default_method" &&
89
          isset($ctrl_method_list[$method])) {
90
        $controller_obj->$ctrl_method_list[$method] (array_slice($parts, 2));
91
        return;
7 by Gustav Hartvigsson
* a few more functions...
92
      }
4 by Gustav Hatvigsson
* Added FourOhFour.php
93
      $controller_obj->default_method (NULL);
94
      return;
3 by Gustav Hatvigsson
* Made the router a class
95
    }
7 by Gustav Hartvigsson
* a few more functions...
96
4 by Gustav Hatvigsson
* Added FourOhFour.php
97
    $controller_obj = new $this->four_oh_four_controller ();
98
    $controller_obj->default_method ($url);
7 by Gustav Hartvigsson
* a few more functions...
99
3 by Gustav Hatvigsson
* Made the router a class
100
  }
1 by Gustav Hartvigsson
* initial code
101
}