/+junk/StupidMVC

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/StupidMVC

« back to all changes in this revision

Viewing changes to Common/Router.php

  • Committer: Gustav Hartvigsson
  • Date: 2016-03-16 16:41:50 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20160316164150-zbrppdt5e743pzu5
* a few more functions...

Show diffs side-by-side

added added

removed removed

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