/+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
8 by Gustav Hartvigsson
woops
32
  public function
33
  register_default_controller ($controller_name) {
3 by Gustav Hatvigsson
* Made the router a class
34
    if (isset($this->default_controller)) {
35
      return;
36
    }
37
    $this->default_controller = $controller_name;
38
    $this->register_controller ($controller_name);
39
  }
7 by Gustav Hartvigsson
* a few more functions...
40
41
  public function
42
  route () {
43
44
3 by Gustav Hatvigsson
* Made the router a class
45
    var_dump ($this->list_controllers);
7 by Gustav Hartvigsson
* a few more functions...
46
3 by Gustav Hatvigsson
* Made the router a class
47
    $url = "";
48
    $method = "default_method";
49
    $ctrl = $this->default_controller;
50
    $path = "";
7 by Gustav Hartvigsson
* a few more functions...
51
3 by Gustav Hatvigsson
* Made the router a class
52
    $request_url = '';
53
    if (isset($_SERVER['REQUEST_URI'])) {
54
      $request_url = $_SERVER['REQUEST_URI'];
55
    }
56
    //echo "Request URL: " . $request_url . "\n";
57
    $script_url = '';
58
    if (isset($_SERVER['PHP_SELF'])){
59
      $script_url = $_SERVER['PHP_SELF'];
60
    }
61
    //echo "Script URL: " . $script_url . "\n";
7 by Gustav Hartvigsson
* a few more functions...
62
3 by Gustav Hatvigsson
* Made the router a class
63
    if($request_url != $script_url) {
4 by Gustav Hatvigsson
* Added FourOhFour.php
64
      $url = trim(preg_replace('/'.
65
        str_replace('/', '\/',
66
          str_replace('index.php', '', $script_url))
67
            .'/', '', $request_url, 1), '/');
3 by Gustav Hatvigsson
* Made the router a class
68
      //echo "url: " . $url . "\n";
69
    }
7 by Gustav Hartvigsson
* a few more functions...
70
3 by Gustav Hatvigsson
* Made the router a class
71
    $parts = explode ('/', $url );
7 by Gustav Hartvigsson
* a few more functions...
72
3 by Gustav Hatvigsson
* Made the router a class
73
    var_dump ($parts);
7 by Gustav Hartvigsson
* a few more functions...
74
3 by Gustav Hatvigsson
* Made the router a class
75
    if (isset ($parts[0]) && $parts[0] != '') {
76
      $ctrl = $parts[0];
77
    }
7 by Gustav Hartvigsson
* a few more functions...
78
3 by Gustav Hatvigsson
* Made the router a class
79
    if (isset ($parts[1]) && $parts[1] != '') {
80
      $method = $parts[1];
81
    }
7 by Gustav Hartvigsson
* a few more functions...
82
3 by Gustav Hatvigsson
* Made the router a class
83
    var_dump ($ctrl);
84
    var_dump ($method);
7 by Gustav Hartvigsson
* a few more functions...
85
3 by Gustav Hatvigsson
* Made the router a class
86
    if(isset ($this->list_controllers[$ctrl])) {
87
      $controller_obj = new $ctrl ();
88
      $ctrl_method_list = $controller_obj->get_methods();
89
      if ($method != "default_method" &&
90
          isset($ctrl_method_list[$method])) {
91
        $controller_obj->$ctrl_method_list[$method] (array_slice($parts, 2));
92
        return;
7 by Gustav Hartvigsson
* a few more functions...
93
      }
4 by Gustav Hatvigsson
* Added FourOhFour.php
94
      $controller_obj->default_method (NULL);
95
      return;
3 by Gustav Hatvigsson
* Made the router a class
96
    }
7 by Gustav Hartvigsson
* a few more functions...
97
4 by Gustav Hatvigsson
* Added FourOhFour.php
98
    $controller_obj = new $this->four_oh_four_controller ();
99
    $controller_obj->default_method ($url);
7 by Gustav Hartvigsson
* a few more functions...
100
3 by Gustav Hatvigsson
* Made the router a class
101
  }
1 by Gustav Hartvigsson
* initial code
102
}