@@ -67,7 +67,7 @@ class Core
67
67
// The parameters extracted from the request URI when this route item is matched
68
68
private $ params ;
69
69
70
- // The regular expression used to match the request URI against the path of this route item
70
+ // Theregular expression used to match the request URI against the path of this route item
71
71
private $ pathRegex ;
72
72
73
73
public function __construct ($ method , $ path , $ controller )
@@ -143,32 +143,38 @@ public function match($request_method, $request_uri)
143
143
144
144
public function execute ()
145
145
{
146
- list ($ controller , $ method ) = explode ('@ ' , $ this ->controller );
147
- $ controllerClass = "Monster \\App \\Controllers \\" . $ controller ;
146
+ if (is_callable ($ this ->controller )) {
147
+ // If the controller is a closure, execute it directly
148
+ call_user_func_array ($ this ->controller , $ this ->params );
149
+ } else {
150
+ // If the controller is a string, parse it into a controller and method to execute
151
+ list ($ controller , $ method ) = explode ('@ ' , $ this ->controller );
152
+ $ controllerClass = "Monster \\App \\Controllers \\" . $ controller ;
148
153
149
- // Cache the value of class_exists and call_user_func_array
150
- static $ classExists = [];
151
- static $ callUserFuncArray = [];
154
+ // Cache the value of class_exists and call_user_func_array
155
+ static $ classExists = [];
156
+ static $ callUserFuncArray = [];
152
157
153
- if (!isset ($ classExists [$ controllerClass ])) {
154
- $ classExists [$ controllerClass ] = class_exists ($ controllerClass );
155
- }
158
+ if (!isset ($ classExists [$ controllerClass ])) {
159
+ $ classExists [$ controllerClass ] = class_exists ($ controllerClass );
160
+ }
156
161
157
- if ($ classExists [$ controllerClass ]) {
158
- if (!isset ($ callUserFuncArray [$ controllerClass ])) {
159
- $ callUserFuncArray [$ controllerClass ] = function ($ controllerInstance , $ method , $ params ) {
160
- if ($ params ) {
161
- $ controllerInstance ->$ method (...$ params );
162
- } else {
163
- $ controllerInstance ->$ method ();
164
- }
165
- };
162
+ if ($ classExists [$ controllerClass ]) {
163
+ if (!isset ($ callUserFuncArray [$ controllerClass ])) {
164
+ $ callUserFuncArray [$ controllerClass ] = function ($ controllerInstance , $ method , $ params ) {
165
+ if ($ params ) {
166
+ $ controllerInstance ->$ method (...$ params );
167
+ } else {
168
+ $ controllerInstance ->$ method ();
169
+ }
170
+ };
171
+ }
172
+ $ controllerInstance = new $ controllerClass ;
173
+ $ callUserFuncArray [$ controllerClass ]($ controllerInstance , $ method , $ this ->params );
174
+ } else {
175
+ http_response_code (500 );
176
+ echo "Internal Server Error: Controller class not found " ;
166
177
}
167
- $ controllerInstance = new $ controllerClass ;
168
- $ callUserFuncArray [$ controllerClass ]($ controllerInstance , $ method , $ this ->params );
169
- } else {
170
- http_response_code (500 );
171
- echo "Internal Server Error: Controller class not found " ;
172
178
}
173
179
}
174
180
}
0 commit comments