|
1 | 1 | <?php |
| 2 | +// Main router for React SPA with PHP fallbacks |
| 3 | +$request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); |
| 4 | +$spa_path = __DIR__ . '/../spa/dist'; |
| 5 | + |
| 6 | +// Handle static assets (JS, CSS, images, etc.) |
| 7 | +if (preg_match('/\.(js|css|png|jpg|jpeg|gif|svg|ico)$/', $request_uri)) { |
| 8 | + $file_path = $spa_path . $request_uri; |
| 9 | + if (file_exists($file_path)) { |
| 10 | + $ext = pathinfo($file_path, PATHINFO_EXTENSION); |
| 11 | + $content_types = [ |
| 12 | + 'js' => 'application/javascript', |
| 13 | + 'css' => 'text/css', |
| 14 | + 'png' => 'image/png', |
| 15 | + 'jpg' => 'image/jpeg', |
| 16 | + 'jpeg' => 'image/jpeg', |
| 17 | + 'gif' => 'image/gif', |
| 18 | + 'svg' => 'image/svg+xml', |
| 19 | + 'ico' => 'image/x-icon' |
| 20 | + ]; |
| 21 | + |
| 22 | + if (isset($content_types[$ext])) { |
| 23 | + header('Content-Type: ' . $content_types[$ext]); |
| 24 | + } |
| 25 | + readfile($file_path); |
| 26 | + exit; |
| 27 | + } else { |
| 28 | + // Asset not found |
| 29 | + http_response_code(404); |
| 30 | + echo "Asset not found: " . $request_uri; |
| 31 | + exit; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// Handle API routes |
| 36 | +if (strpos($request_uri, '/api/') === 0) { |
| 37 | + $api_file = __DIR__ . $request_uri; |
| 38 | + if (file_exists($api_file)) { |
| 39 | + include $api_file; |
| 40 | + exit; |
| 41 | + } else { |
| 42 | + http_response_code(404); |
| 43 | + echo json_encode(['error' => 'API endpoint not found']); |
| 44 | + exit; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Handle POST data for React app |
| 49 | +if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST)) { |
| 50 | + // Store POST data in session for React app to display |
| 51 | + session_start(); |
| 52 | + $_SESSION['post_data'] = $_POST; |
| 53 | + |
| 54 | + // Redirect back to index with a flag |
| 55 | + header('Location: /?posted=1'); |
| 56 | + exit; |
| 57 | +} |
| 58 | + |
| 59 | +// Handle special PHP routes that still need server-side processing |
2 | 60 | if (!headers_sent()) header('Content-Type: text/html; charset=UTF-8'); |
3 | 61 |
|
4 | | -require_once('glue.php'); |
5 | | -require_once('data.php'); |
6 | | -require_once('controllers.php'); |
7 | | - |
8 | | -$urls = array( |
9 | | - '/' => 'index', |
10 | | - '/info' => 'info', |
11 | | - '/cookies' => 'cookies', |
12 | | - '/cookies2' => 'cookiesHeader', |
13 | | - '/search.*' => 'search', |
14 | | - '/login' => 'login', |
15 | | - '/redirect' => 'redirect', |
16 | | - '/redirect2' => 'redirect2', |
17 | | - '/redirect3' => 'redirect3', |
18 | | - '/redirect_long' => 'redirect_long', |
19 | | - '/redirect4' => 'redirect4', |
20 | | - '/redirect_params' => 'redirect_params', |
21 | | - '/redirect_interval' => 'redirect_interval', |
22 | | - '/redirect_header_interval' => 'redirect_header_interval', |
23 | | - '/redirect_self' => 'redirect_self', |
24 | | - '/relative_redirect' => 'redirect_relative', |
25 | | - '/relative/redirect' => 'redirect_relative', |
26 | | - '/redirect_twice' => 'redirect_twice', |
27 | | - '/relative/info' => 'info', |
28 | | - '/somepath/redirect_base_uri_has_path' => 'redirect_base_uri_has_path', |
29 | | - '/somepath/redirect_base_uri_has_path_302' => 'redirect_base_uri_has_path_302', |
30 | | - '/somepath/info' => 'info', |
31 | | - '/facebook\??.*' => 'facebookController', |
32 | | - '/form/(.*?)(#|\?.*?)?' => 'form', |
33 | | - '/articles\??.*' => 'articles', |
34 | | - '/auth' => 'httpAuth', |
35 | | - '/register' => 'register', |
36 | | - '/content-iso' => 'contentType1', |
37 | | - '/content-cp1251' => 'contentType2', |
38 | | - '/unset-cookie' => 'unsetCookie', |
39 | | - '/external_url' => 'external_url', |
40 | | - '/spinner' => 'spinner', |
41 | | - '/iframe' => 'iframe', |
42 | | - '/iframes' => 'iframes', |
43 | | - '/iframe_nested' => 'iframe_nested', |
44 | | - '/dynamic' => 'dynamic', |
45 | | - '/timeout' => 'timeout', |
46 | | - '/download' => 'download', |
47 | | - '/basic_auth' => 'basic_auth', |
48 | | - '/image' => 'basic_image', |
49 | | - '/invisible_elements' => 'invisible_elements' |
50 | | -); |
51 | | - |
52 | | -glue::stick($urls); |
| 62 | +// Routes that need special PHP processing |
| 63 | +$special_routes = [ |
| 64 | + '/cookies', '/cookies2', '/login', '/auth', '/register', |
| 65 | + '/content-iso', '/content-cp1251', '/unset-cookie', |
| 66 | + '/download', '/basic_auth', '/redirect', '/redirect2', |
| 67 | + '/redirect3', '/redirect_long', '/redirect4', |
| 68 | + '/redirect_params', '/redirect_interval', |
| 69 | + '/redirect_header_interval', '/redirect_self', |
| 70 | + '/relative_redirect', '/relative/redirect', '/redirect_twice', |
| 71 | + '/somepath/redirect_base_uri_has_path', |
| 72 | + '/somepath/redirect_base_uri_has_path_302', |
| 73 | + '/facebook', '/articles', '/external_url', |
| 74 | + '/iframe', '/iframes', '/iframe_nested', '/dynamic', |
| 75 | + '/timeout', '/image', '/invisible_elements' |
| 76 | +]; |
| 77 | + |
| 78 | +foreach ($special_routes as $route) { |
| 79 | + if (strpos($request_uri, $route) === 0) { |
| 80 | + // Load the original PHP routing for these special cases |
| 81 | + require_once('glue.php'); |
| 82 | + require_once('data.php'); |
| 83 | + require_once('controllers.php'); |
| 84 | + |
| 85 | + $urls = array( |
| 86 | + '/' => 'index', |
| 87 | + '/info' => 'info', |
| 88 | + '/cookies' => 'cookies', |
| 89 | + '/cookies2' => 'cookiesHeader', |
| 90 | + '/search.*' => 'search', |
| 91 | + '/login' => 'login', |
| 92 | + '/redirect' => 'redirect', |
| 93 | + '/redirect2' => 'redirect2', |
| 94 | + '/redirect3' => 'redirect3', |
| 95 | + '/redirect_long' => 'redirect_long', |
| 96 | + '/redirect4' => 'redirect4', |
| 97 | + '/redirect_params' => 'redirect_params', |
| 98 | + '/redirect_interval' => 'redirect_interval', |
| 99 | + '/redirect_header_interval' => 'redirect_header_interval', |
| 100 | + '/redirect_self' => 'redirect_self', |
| 101 | + '/relative_redirect' => 'redirect_relative', |
| 102 | + '/relative/redirect' => 'redirect_relative', |
| 103 | + '/redirect_twice' => 'redirect_twice', |
| 104 | + '/relative/info' => 'info', |
| 105 | + '/somepath/redirect_base_uri_has_path' => 'redirect_base_uri_has_path', |
| 106 | + '/somepath/redirect_base_uri_has_path_302' => 'redirect_base_uri_has_path_302', |
| 107 | + '/somepath/info' => 'info', |
| 108 | + '/facebook\??.*' => 'facebookController', |
| 109 | + '/form/(.*?)(#|\?.*?)?' => 'form', |
| 110 | + '/articles\??.*' => 'articles', |
| 111 | + '/auth' => 'httpAuth', |
| 112 | + '/register' => 'register', |
| 113 | + '/content-iso' => 'contentType1', |
| 114 | + '/content-cp1251' => 'contentType2', |
| 115 | + '/unset-cookie' => 'unsetCookie', |
| 116 | + '/external_url' => 'external_url', |
| 117 | + '/spinner' => 'spinner', |
| 118 | + '/iframe' => 'iframe', |
| 119 | + '/iframes' => 'iframes', |
| 120 | + '/iframe_nested' => 'iframe_nested', |
| 121 | + '/dynamic' => 'dynamic', |
| 122 | + '/timeout' => 'timeout', |
| 123 | + '/download' => 'download', |
| 124 | + '/basic_auth' => 'basic_auth', |
| 125 | + '/image' => 'basic_image', |
| 126 | + '/invisible_elements' => 'invisible_elements' |
| 127 | + ); |
| 128 | + |
| 129 | + glue::stick($urls); |
| 130 | + exit; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// For all other routes (React SPA routes), serve the index.html |
| 135 | +$index_file = $spa_path . '/index.html'; |
| 136 | +if (file_exists($index_file)) { |
| 137 | + readfile($index_file); |
| 138 | +} else { |
| 139 | + echo "React SPA not built. Please run 'npm run build' in test/data/spa directory."; |
| 140 | +} |
0 commit comments