Skip to content

Commit b70f6c7

Browse files
minor changes
1 parent e68da0d commit b70f6c7

File tree

7 files changed

+64
-27
lines changed

7 files changed

+64
-27
lines changed

index.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,4 @@
22

33
require_once __DIR__ . '/init.php';
44

5-
$request_uri = $_SERVER['REQUEST_URI'];
6-
$parsed_uri = parse_url($request_uri);
7-
$req_path = $parsed_uri['path'];
8-
$req_query = isset($parsed_uri['query']) ? $parsed_uri['query'] : null;
9-
105
Router::route($req_path);

init.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@
1313
$dotenv->safeLoad();
1414

1515
//// CONSTANTS
16-
// Statics
17-
define('NODE_MODULE', $_ENV['APP_URL'] . '/node_modules/');
18-
define('APP_STATIC', $_ENV['APP_URL'] . '/assets/');
16+
// App Info
17+
define('APP_NAME', $_ENV['APP_NAME']);
18+
define('APP_DEBUG', $_ENV['APP_DEBUG']);
19+
define('APP_URL', $_ENV['APP_URL']);
20+
define('APP_ROOT', $_ENV['APP_ROUTE_ROOT']);
21+
define('APP_API_ROOT', $_ENV['API_ROUTE_ROOT']);
22+
// Static files link
23+
define('NODE_MODULE', APP_URL . '/node_modules/');
24+
define('APP_STATIC', APP_URL . '/assets/');
1925
define('APP_IMAGES', APP_STATIC . 'images/');
2026
define('APP_STYLES', APP_STATIC . 'css/');
2127
define('APP_JS', APP_STATIC . 'js/');
2228
// Directories
2329
define('APP_BASE_DIR', __DIR__ . '/');
24-
define('API_VIEW', __DIR__ . $_ENV['API_ROUTE_ROOT'] . '/');
30+
define('API_VIEW', __DIR__ . APP_API_ROOT . '/');
2531
define('APP_VIEW', __DIR__ . '/views/');
2632
define('APP_ERROR_PAGES', __DIR__ . '/errors/');
2733
define('APP_FUNCTION', __DIR__ . '/scripts/functions/');
@@ -30,6 +36,20 @@
3036
define('APP_COMPONENTS', APP_TEMPLATES . 'components/');
3137

3238

39+
// Parse Request
40+
$request_uri = $_SERVER['REQUEST_URI'];
41+
$parsed_uri = parse_url($request_uri);
42+
$req_path = $parsed_uri['path'];
43+
$req_query_string = isset($parsed_uri['query']) ? $parsed_uri['query'] : null;
44+
parse_str($req_query_string, $req_query);
45+
46+
define('REQUEST', [
47+
'route' => preg_replace('/' . preg_quote(APP_ROOT, '/') . '/', '', $req_path, 1),
48+
'method' => strtolower($_SERVER['REQUEST_METHOD']),
49+
'query' => $req_query,
50+
'request' => ($_SERVER['REQUEST_METHOD'] === 'POST') ? $_POST : $_GET
51+
]);
52+
3353
// Requires
3454
require_once APP_FUNCTION . 'functions.script.php';
3555
require_once APP_FUNCTION . 'router.script.php';

routes/api.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
define('API_ROUTES', [
24-
'ERR' => $API_ERROR_ROUTES,
25-
'GET' => $API_GET_ROUTES,
26-
'POST' => $API_POST_ROUTES,
24+
'err' => $API_ERROR_ROUTES,
25+
'get' => $API_GET_ROUTES,
26+
'post' => $API_POST_ROUTES,
2727
]);

routes/app.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
'/' => ['index.php', 'root'],
1717
];
1818

19-
$APP_POST_ROUTES = [];
19+
$APP_POST_ROUTES = [
20+
'/' => ['index.php', 'root'],
21+
];
2022

2123

2224
define('APP_ROUTES', [
23-
'ERR' => $APP_ERROR_ROUTES,
24-
'GET' => $APP_GET_ROUTES,
25-
'POST' => $APP_POST_ROUTES,
25+
'err' => $APP_ERROR_ROUTES,
26+
'get' => $APP_GET_ROUTES,
27+
'post' => $APP_POST_ROUTES,
2628
]);

scripts/functions/functions.script.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function flatenAssocArray($array, $sql = true)
8787
*/
8888
function set_cookie(string $name, $value, int $expiry = 30): void
8989
{
90-
setcookie($name, $value, $expiry, $_ENV['APP_ROUTE_ROOT'] . '/');
90+
setcookie($name, $value, $expiry, APP_ROOT . '/');
9191
}
9292

9393
/**
@@ -99,13 +99,13 @@ function set_cookie(string $name, $value, int $expiry = 30): void
9999
function enroute($name)
100100
{
101101
$parts = explode(':', $name);
102-
$method = strtoupper($parts[0]);
102+
$method = $parts[0];
103103

104104
array_shift($parts);
105105
$needle = implode(':', $parts);
106106
$heystack = APP_ROUTES[$method];
107107

108-
return $_ENV['APP_ROUTE_ROOT'] . findKeyByNeedle($heystack, $needle);
108+
return APP_ROOT . findKeyByNeedle($heystack, $needle);
109109
}
110110

111111
/**

scripts/functions/router.script.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Router
1414
*/
1515
private static function app_route($path, $vars = [])
1616
{
17-
$routes = APP_ROUTES[$_SERVER['REQUEST_METHOD']];
17+
$routes = APP_ROUTES[REQUEST['method']];
1818

1919
$flash_msg = null;
2020
if (isset($_SESSION['flash_msg'])) {
@@ -29,7 +29,7 @@ private static function app_route($path, $vars = [])
2929
extract($vars, EXTR_SKIP);
3030
require APP_VIEW . $routes[$path][0];
3131
} else {
32-
require APP_ERROR_PAGES . APP_ROUTES['ERR']['404'];
32+
require APP_ERROR_PAGES . APP_ROUTES['err']['404'];
3333
}
3434

3535
// Include the footer partial
@@ -44,11 +44,11 @@ private static function app_route($path, $vars = [])
4444
*/
4545
private static function api_route($path)
4646
{
47-
$routes = API_ROUTES[$_SERVER['REQUEST_METHOD']];
47+
$routes = API_ROUTES[REQUEST['method']];
4848
if (array_key_exists($path, $routes)) {
4949
header("Content-Type: application/json");
5050
require API_VIEW . $routes[$path][0];
51-
} else require APP_ERROR_PAGES . API_ROUTES['ERR']['404'];
51+
} else require APP_ERROR_PAGES . API_ROUTES['err']['404'];
5252
}
5353

5454
/**
@@ -60,7 +60,7 @@ private static function api_route($path)
6060
*/
6161
public static function route($uri, $vars = [])
6262
{
63-
$path = preg_replace('/' . preg_quote($_ENV['APP_ROUTE_ROOT'], '/') . '/', '', $uri, 1);
63+
$path = preg_replace('/' . preg_quote(APP_ROOT, '/') . '/', '', $uri, 1);
6464
$parts = explode('/', $path);
6565

6666
if ($parts[1] == 'api') {

views/index.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
<div class="container pt-3">
2-
<h2>HOME PAGE</h1>
3-
<p>Welcome to the Home page, <b>Route:</b> <code><?= enroute('get:root') ?></code></p>
4-
<small class="text-secondary">Generated from <?= $_ENV['APP_NAME'] ?></small>
2+
<h2>HOME PAGE</h2>
3+
<p>Welcome to the Home page @ <a href="<?= enroute('get:root') ?>"><?= APP_URL ?></a>.</p>
4+
<div class="rounded-1 mt-1 mb-1 p-3" style="background-color: #ededed;">
5+
<table>
6+
<tr>
7+
<th class="pe-3 text-end">route</th>
8+
<td> <code><?= enroute('get:root') ?></code></td>
9+
</tr>
10+
<tr>
11+
<th class="pe-3 text-end">defined route</th>
12+
<td> <code><?= REQUEST['route'] ?></code></td>
13+
</tr>
14+
<tr>
15+
<th class="pe-3 text-end">method</th>
16+
<td> <code><?= strtoupper(REQUEST['method']) ?></code></td>
17+
</tr>
18+
<tr>
19+
<th class="pe-3 text-end">query</th>
20+
<td> <code><?= json_encode(REQUEST['query'], JSON_PRETTY_PRINT) ?></code></td>
21+
</tr>
22+
</table>
23+
</div>
24+
<small class="text-secondary">Generated from <?= APP_NAME ?></small>
525
</div>

0 commit comments

Comments
 (0)