-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathroute.php
More file actions
152 lines (131 loc) · 5.56 KB
/
route.php
File metadata and controls
152 lines (131 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* route.php
* Routing for PHP built-in web server
*
* Example (windows):
* php -c "C:\PHP_INSTALL_DIR\php.ini" -S localhost:8000 "C:\PATH_OF_WEBMVCFRAMEWORK\route.php" -t "C:\PATH_OF_WEBMVCFRAMEWORK"
* (for Linux or Mac replace path)
*
* Usage example of route.php is inside mvc_bootstrap.bat batch script
*
* This file implements HTTP routing when using PHP built in web server instead of Apache HTTP server.
* It can detect both application or content centric HTTP request.
* 1) When application centric:
* It provides the autoloading features of classes and the MVC objects creations, by using
* framework\Loader and framework\Dispatcher classes (depending on the requested URL).
* 2) When content centric:
* Just serve the content of requested resource (or execute a standard php file)
*
* @filesource index.php
* @author Rosario Carvello <rosario.carvello@gmail.com>
* @version GIT:v1.0.0
* @copyright (c) 2016-2025 Rosario Carvello <rosario.carvello@gmail.com> - All rights reserved. See License.txt file
* @license BSD Clause 3 License
* @license https://opensource.org/licenses/BSD-3-Clause This software is distributed under BSD-3-Clause Public License
*/
// Enable error reporting. Optionally disable notices by replacing with: error_reporting(E_ALL & ~E_DEPRECATED)
error_reporting(E_ALL);
// Only when using PHP built-in web server
chdir(__DIR__);
// Console Output
$green = "\033[32m";
$yellow = "\033[33m";
$cyan = "\033[36m";
$red = "\033[31m";
$blue = "\033[34m";
$reset = "\033[0m";
$uri = $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];
$time = date('Y-m-d H:i:s');
file_put_contents('php://stdout', "\033[33m $time \033[36m $method \033[32m $uri \033[0m \n");
// Parse the request
$url_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$filePath = ltrim($url_path, '/');
// If request is an existing physical resource handle it with the server (CONTENT CENTRIC REQUEST)
if ($filePath && is_file($filePath)) {
return false;
}
// If no physical resource is requested then is a virtual resource to be managed by the Framework (APPLICATION CENTRIC REQUEST)
// Set path CONSTANT (required by the Framework)
define("RELATIVE_PATH", "");
// Common initializations and configurations loading
// Note: for changing framework or application setting see config folder.
header('Content-Type: text/html; charset=utf-8');
// Load Framework main configuration
include_once RELATIVE_PATH . "config/framework.config.php";
// Starting and securing session
session_start();
session_regenerate_id();
// Set url variable (required by th Framework)
$_GET['url'] = ltrim($url_path, '/');
// Use of Framework classes for handling request
use framework\Dispatcher;
use framework\Loader;
try {
// Set classes autoloader simply by instantiating the framework Loader
$loader = new Loader();
// Create a Dispatcher to dispatch URL request to the appropriate user controller
$dispatcher = new Dispatcher();
$dispatcher->dispatch();
} catch (\Throwable $th) {
//throw throwable;
printCatch($th);
} catch (Exception $e) {
//throw exception;
printCatch($e);
}
function printCatch($e)
{
$html = <<<HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Error</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap 5 CDN -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<div class="container">
<body class="bg-white text-light">
<div class="container py-5">
<div class="alert bg-dark shadow-lg">
<h1 class="display-5 fw-bold text-danger">
<i class="bi bi-bug-fill"></i> Error!
</h1>
<hr class="border-light">
<p>
<span class="badge bg-warning text-dark">File</span>
<code class="h3">{$e->getFile()}</code>
</p>
<p>
<span class="badge bg-info text-dark">Line</span>
<code class="h2" >{$e->getLine()}</code>
</p>
<hr>
<p class="lead text-light">
<span class="badge bg-danger text-dark">Error info</span><br>
<code class="text-warning h2">{$e->getMessage()}</code>
</p>
<button class="btn btn-outline-light mt-3" onclick="location.reload()">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-clockwise" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2z"/>
<path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466"/>
</svg>
Reload page
</button>
</div>
</div>
<div class="alert bg-dark shadow-lg">
<div class="text-center text-yellow bg-dark">
PHP WEB MVC Framework - [
<a href="https://github.com/rcarvello/webmvcframework/wiki">Wiki Pages</a> |
<a href="https://github.com/rcarvello/webmvcframework">GitHub</a> ]
</div>
</body>
</div>
</html>
HTML;
echo $html;
}