8
8
*/
9
9
class Router
10
10
{
11
- private $ routes = [];
12
- private $ params = [];
13
-
14
- /**
15
- * Method used to load routes.php file
16
- * @param string $file Path to file
17
- * @return Router Instance of router in order to use the other methods
18
- */
19
- public static function load (string $ file ) : Router
20
- {
21
- $ router = new static ;
22
-
23
- require_once $ file ;
24
-
25
- return $ router ;
26
- }
27
-
28
- /**
29
- * Register a route
30
- * Process to convert the route (string) to a regular expression in order to have a more flexible router
31
- * @param string $route Route string, either full route or regular expression
32
- * @param array $params This array stores 'controller' and 'action of each route (if passed)
33
- * @return void
34
- */
35
- public function addRoute (string $ route , array $ params = []) : void
36
- {
37
- // Escape forward slashes
38
- $ route = preg_replace ('/\// ' , '\\/ ' , $ route );
39
-
40
- // Convert variables e.g. {controller}
41
- $ route = preg_replace ('/\{([a-z0-9]+)\}/ ' , '(?P<1円>[a-z0-9-]+) ' , $ route );
42
-
43
- // Convert variables with custom regular expressions e.g. {id:\d+} for numbers
44
- $ route = preg_replace ('/\{([a-z0-9]+):([^\}]+)\}/ ' , '(?P<1円>2円) ' , $ route );
45
-
46
- // Add start and end delimeter
47
- $ route = '/^ ' . $ route . '$/i ' ;
48
-
49
- $ this ->routes [$ route ] = $ params ;
50
- }
51
-
52
- /**
53
- * This is where you set the controller and action taken from the URI (like 'posts/add')
54
- * ... and save those into $this->params
55
- * This method is called in routes.php
56
- * @param string $uri Full route like 'task/add-task'
57
- * @return void
58
- */
59
- public function setParams (string $ uri ) : void
60
- {
61
- // Store parameters for current 'controller' and 'action'
62
- foreach ($ this ->routes as $ route => $ params )
63
- {
64
- if (preg_match ($ route , $ uri , $ matches ))
65
- {
66
-
67
- foreach ($ matches as $ key => $ match )
68
- {
69
- if (is_string ($ key ))
70
- {
71
- if ($ key === 'controller ' )
72
- {
73
- $ match = ucwords ($ match );
74
- }
75
-
76
- $ params [$ key ] = $ match ;
77
- }
78
- }
79
-
80
- $ this ->params = $ params ;
81
- }
82
- }
83
- }
84
-
85
- /**
86
- * Call requested controller->action()
87
- * At this point $this->params is = ['controller' => 'MyController', 'action' => 'desiredMethod', 'id' => 1]
88
- * So first check if such 'controller' exists and 'action' is a callable method
89
- * If so, then unset 'controller' and 'action', which leaves $this->params = ['id' => 1]
90
- * And last, the desired function is called with all the parameters (array) like 'id'...
91
- * ... and this can be used or not if the method doesn't receive any arguments
92
- * @return void
93
- */
94
- public function redirect () : void
95
- {
96
- $ controller = $ this ->getNamespace () . $ this ->params ['controller ' ];
97
- $ action = $ this ->capitalizeAction ($ this ->params ['action ' ]);
98
-
99
- if (class_exists ($ controller ))
100
- {
101
- $ controller = new $ controller ;
102
- unset($ this ->params ['controller ' ]);
103
-
104
- if (is_callable ([$ controller , $ action ]))
105
- {
106
- unset($ this ->params ['action ' ]);
107
- unset($ this ->params ['namespace ' ]);
108
- }
109
- else
110
- {
111
- die ('Page not found. ' );
112
- }
113
- }
114
- else
115
- {
116
- header ('location: ' . URLROOT );
117
- }
118
-
119
- call_user_func_array ([$ controller , $ action ], [$ this ->params ]);
120
- }
121
-
122
- /**
123
- * Get the namespace for the requested controller class
124
- * @return string $namespace String for the complete namespace
125
- */
126
- private function getNamespace () : string
127
- {
128
- $ namespace = '\\Controllers \\' ;
129
-
130
- if (array_key_exists ('namespace ' , $ this ->params ))
131
- {
132
- $ namespace .= $ this ->params ['namespace ' ] . '\\' ;
133
- }
134
-
135
- return $ namespace ;
136
- }
137
-
138
- /**
139
- * Camel case the 'action' string
140
- * If in the URL the action is something like '{controller}/add-new-task'...
141
- * ... change it to 'addNewTask' in order to make it match with a class method
142
- * @param string $action Get the 'action' from the URL
143
- * @return string Camel Cased 'action' string
144
- */
145
- private function capitalizeAction (string $ action ) : string
146
- {
147
- $ action = explode ('- ' , $ action );
148
-
149
- for ($ i =1 ; $ i < count ($ action ); $ i ++){
150
- $ action [$ i ] = ucwords ($ action [$ i ]);
151
- }
152
-
153
- return implode ($ action );
154
- }
11
+ private $ routes = [];
12
+ private $ params = [];
13
+
14
+ /**
15
+ * Method used to load routes.php file
16
+ * @param string $file Path to file
17
+ * @return Router Instance of router in order to use the other methods
18
+ */
19
+ public static function load (string $ file ) : Router
20
+ {
21
+ $ router = new static ;
22
+
23
+ require_once $ file ;
24
+
25
+ return $ router ;
26
+ }
27
+
28
+ /**
29
+ * Register a route
30
+ * Process to convert the route (string) to a regular expression in order to have a more flexible router
31
+ * @param string $route Route string, either full route or regular expression
32
+ * @param array $params This array stores 'controller' and 'action of each route (if passed)
33
+ * @return void
34
+ */
35
+ public function addRoute (string $ route , array $ params = []) : void
36
+ {
37
+ // Escape forward slashes
38
+ $ route = preg_replace ('/\// ' , '\\/ ' , $ route );
39
+
40
+ // Convert variables e.g. {controller}
41
+ $ route = preg_replace ('/\{([a-z0-9]+)\}/ ' , '(?P<1円>[a-z0-9-]+) ' , $ route );
42
+
43
+ // Convert variables with custom regular expressions e.g. {id:\d+} for numbers
44
+ $ route = preg_replace ('/\{([a-z0-9]+):([^\}]+)\}/ ' , '(?P<1円>2円) ' , $ route );
45
+
46
+ // Add start and end delimeter
47
+ $ route = '/^ ' . $ route . '$/i ' ;
48
+
49
+ $ this ->routes [$ route ] = $ params ;
50
+ }
51
+
52
+ /**
53
+ * This is where you set the controller and action taken from the URI (like 'posts/add')
54
+ * ... and save those into $this->params
55
+ * This method is called in routes.php
56
+ * @param string $uri Full route like 'task/add-task'
57
+ * @return void
58
+ */
59
+ public function setParams (string $ uri ) : void
60
+ {
61
+ // Store parameters for current 'controller' and 'action'
62
+ foreach ($ this ->routes as $ route => $ params )
63
+ {
64
+ if (preg_match ($ route , $ uri , $ matches ))
65
+ {
66
+
67
+ foreach ($ matches as $ key => $ match )
68
+ {
69
+ if (is_string ($ key ))
70
+ {
71
+ if ($ key === 'controller ' )
72
+ {
73
+ $ match = ucwords ($ match );
74
+ }
75
+
76
+ $ params [$ key ] = $ match ;
77
+ }
78
+ }
79
+
80
+ $ this ->params = $ params ;
81
+ }
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Call requested controller->action()
87
+ * At this point $this->params is = ['controller' => 'MyController', 'action' => 'desiredMethod', 'id' => 1]
88
+ * So first check if such 'controller' exists and 'action' is a callable method
89
+ * If so, then unset 'controller' and 'action', which leaves $this->params = ['id' => 1]
90
+ * And last, the desired function is called with all the parameters (array) like 'id'...
91
+ * ... and this can be used or not if the method doesn't receive any arguments
92
+ * @return void
93
+ */
94
+ public function redirect () : void
95
+ {
96
+ $ controller = $ this ->getNamespace () . $ this ->params ['controller ' ];
97
+ $ action = $ this ->capitalizeAction ($ this ->params ['action ' ]);
98
+
99
+ if (class_exists ($ controller ))
100
+ {
101
+ $ controller = new $ controller ;
102
+ unset($ this ->params ['controller ' ]);
103
+
104
+ if (is_callable ([$ controller , $ action ]))
105
+ {
106
+ unset($ this ->params ['action ' ]);
107
+ unset($ this ->params ['namespace ' ]);
108
+ }
109
+ else
110
+ {
111
+ die ('Page not found. ' );
112
+ }
113
+ }
114
+ else
115
+ {
116
+ header ('location: ' . URLROOT );
117
+ }
118
+
119
+ call_user_func_array ([$ controller , $ action ], [$ this ->params ]);
120
+ }
121
+
122
+ /**
123
+ * Get the namespace for the requested controller class
124
+ * @return string $namespace String for the complete namespace
125
+ */
126
+ private function getNamespace () : string
127
+ {
128
+ $ namespace = '\\Controllers \\' ;
129
+
130
+ if (array_key_exists ('namespace ' , $ this ->params ))
131
+ {
132
+ $ namespace .= $ this ->params ['namespace ' ] . '\\' ;
133
+ }
134
+
135
+ return $ namespace ;
136
+ }
137
+
138
+ /**
139
+ * Camel case the 'action' string
140
+ * If in the URL the action is something like '{controller}/add-new-task'...
141
+ * ... change it to 'addNewTask' in order to make it match with a class method
142
+ * @param string $action Get the 'action' from the URL
143
+ * @return string Camel Cased 'action' string
144
+ */
145
+ private function capitalizeAction (string $ action ) : string
146
+ {
147
+ $ action = explode ('- ' , $ action );
148
+
149
+ for ($ i =1 ; $ i < count ($ action ); $ i ++){
150
+ $ action [$ i ] = ucwords ($ action [$ i ]);
151
+ }
152
+
153
+ return implode ($ action );
154
+ }
155
155
}
0 commit comments