@@ -67,7 +67,7 @@ class Core
6767 // The parameters extracted from the request URI when this route item is matched
6868 private $ params ;
6969
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
7171 private $ pathRegex ;
7272
7373 public function __construct ($ method , $ path , $ controller )
@@ -143,32 +143,38 @@ public function match($request_method, $request_uri)
143143
144144 public function execute ()
145145 {
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 ;
148153
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 = [];
152157
153- if (!isset ($ classExists [$ controllerClass ])) {
154- $ classExists [$ controllerClass ] = class_exists ($ controllerClass );
155- }
158+ if (!isset ($ classExists [$ controllerClass ])) {
159+ $ classExists [$ controllerClass ] = class_exists ($ controllerClass );
160+ }
156161
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 " ;
166177 }
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 " ;
172178 }
173179 }
174180}
0 commit comments