11<?php
2+ // filepath: c:\xampp\htdocs\warvilphp\app\init.php
23
34require_once 'core/utils/Loader.php ' ;
45
78// Allow cross-origin requests from any origin
89header ('Access-Control-Allow-Origin: * ' );
910// Allow only specified methods for cross-origin requests
10- header ('Access-Control-Allow-Methods: POST, OPTIONS ' );
11+ header ('Access-Control-Allow-Methods: POST, GET, OPTIONS ' );
1112// Allow the Content-Type header for cross-origin requests
1213header ('Access-Control-Allow-Headers: Content-Type ' );
1314
1415spl_autoload_register (function ($ class_name ) {
15- $ core = ['App ' , 'Router ' , 'RouterApi ' , 'Controller ' , 'Model ' , 'Response ' , 'Request ' , 'Database ' , 'Config ' , 'Layout ' , 'Storage ' ];
16- $ coreUtils = ['Loader ' , 'Helpers ' , 'DateHelper ' ];
16+ $ core = ['App ' , 'Router ' , 'RouterApi ' , 'Controller ' , 'Model ' , 'Response ' , 'Request ' , 'Database ' , 'Config ' , 'Layout ' , 'Storage ' , ' Env ' ];
17+ $ coreUtils = ['Loader ' , 'Helpers ' , 'DateHelper ' , ' UrlHelper ' , ' Redirect ' , ' Session ' ];
1718 $ traits = ['Product ' ];
1819
19- $ routes = ['web ' , 'api ' ];
20-
21- foreach ($ routes as $ route ) {
22- Loader::load ('app/routes/ ' , $ route );
20+ // Load route files - ensure they exist first
21+ $ routeFiles = ['web ' , 'api ' ];
22+ foreach ($ routeFiles as $ route ) {
23+ $ routePath = 'app/routes/ ' . $ route . '.php ' ;
24+ if (file_exists ($ routePath )) {
25+ require_once $ routePath ;
26+ } else {
27+ if (!is_dir ('app/routes ' )) {
28+ mkdir ('app/routes ' , 0755 , true );
29+ }
30+
31+ // Create a default route file
32+ if ($ route === 'web ' ) {
33+ $ content = "<?php \n\nuse app\core\{Router}; \n\nRouter::get('/', 'WelcomeController', 'index'); \n" ;
34+ file_put_contents ($ routePath , $ content );
35+ require_once $ routePath ;
36+ } else if ($ route === 'api ' ) {
37+ $ content = "<?php \n\nuse app\core\{RouterApi}; \n\n// API Routes \n" ;
38+ file_put_contents ($ routePath , $ content );
39+ require_once $ routePath ;
40+ }
41+ }
2342 }
2443
2544 foreach ($ traits as $ trait ) {
3150 }
3251
3352 foreach ($ coreUtils as $ class ) {
34- Loader::load ('app/core/utils ' , $ class );
35- }
36-
37- $ model = 'app/models/ ' . getModelName ($ class_name ) . '.php ' ;
38- $ helper = 'app/core/utils/ ' . $ class_name . '.php ' ;
39- $ core = 'app/core/ ' . $ class_name . '.php ' ;
40-
41- if (file_exists ($ model )) {
42- require_once $ model ;
43- }
44-
45- if (file_exists ($ helper )) {
46- Loader::load ('app/core/utils/ ' , $ class_name );
47- }
48-
49- if (file_exists ($ core )) {
50- Loader::load ('app/core/ ' , $ class_name );
53+ Loader::load ('app/core/utils/ ' , $ class );
5154 }
52- });
55+ });
0 commit comments