Browse code

Controller and Action defined as a routing field

RafaƂ Szklarczyk authored on 23/05/2019 20:12:01
Showing 1 changed files
... ...
@@ -106,18 +106,54 @@ class Application{
106 106
             }
107 107
         }*/
108 108
 
109
+        $field_pattern ='^{(?<field>[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)}$';
110
+
109 111
         if(is_array($controllerAction)){
110 112
             //$this->IncludeControler($ControllerAction['controller']);
113
+            if(isset($controllerAction['module'])){
114
+                if(\preg_match($field_pattern, $controllerAction['module'], $match) ){
115
+                    $module = $this->router->parsedParameters[$match['field']];
116
+                }
117
+                else{
118
+                    $module = $controllerAction['module']."\\";
119
+                }
120
+            }
121
+            else{
122
+                $module = "";
123
+            }
124
+
125
+            
126
+            if(\preg_match($field_pattern, $controllerAction['controller'], $match) ){
127
+                $controller = $this->router->parsedParameters[$match['field']];
128
+            }
129
+            else{
130
+                $controller = $controllerAction['controller'];
131
+            }
132
+            
133
+
111 134
             $controller_class = "\\".$this->applicationDirectory."\\"
112
-                .(isset($controllerAction['module'])?$controllerAction['module']."\\":"")
135
+                .$module
113 136
                 .$this->controllersDirecotry."\\"
114
-                .$controllerAction['controller'];
137
+                .$controller;
115 138
             $controller_object = new $controller_class;
116
-            $controller_action = $controllerAction['action'];
139
+
140
+            if(\preg_match($field_pattern, $controllerAction['action'], $match) ){
141
+                $controller_action = $this->router->parsedParameters[$match['field']];
142
+            }
143
+            else{
144
+                $controller_action = $controllerAction['action'];
145
+            }
146
+            
117 147
         }
118 148
 
119 149
         //fire the action
120 150
         if(isset($controller_object) && isset($controller_action)){
151
+            
152
+            // method not found - 404
153
+            if(!method_exists($controller_object,$controller_action)){
154
+                $this->handle404($this->request);
155
+            }
156
+
121 157
             $this->module = (isset($controllerAction['module'])?$controllerAction['module']:"");
122 158
             $this->controller = $controllerAction['controller'];
123 159
             $this->action = $controller_action;
... ...
@@ -185,6 +221,14 @@ class Application{
185 221
         $this->response->send();
186 222
     }
187 223
 
224
+    public function handle404($request){
225
+        echo "Resource not found!<br>".PHP_EOL;
226
+        echo "<pre>";
227
+        var_dump($request);
228
+        echo "</pre>";
229
+        throw new \Exception("Not implemented!");
230
+    }
231
+
188 232
     public function beforeAction(){
189 233
         if(!empty($this->router->routeInfo->beforeAction)){
190 234
             foreach($this->router->routeInfo->beforeAction as $event_handler){
... ...
@@ -265,8 +309,7 @@ class Application{
265 309
         }
266 310
         // routing path not found -> generate 404 response
267 311
         else{
268
-            echo "Routing not found!<br>".PHP_EOL;
269
-            throw new \Exception("Not implemented!");
312
+            $this->handle404($this->request);
270 313
         }
271 314
  
272 315
     }