Skip to content

Commit d2865e9

Browse files
committed
Please enter the commit message for your changes. Lines starting
with '#' will be ignored, and an empty message aborts the commit. On branch main Your branch and '745/main' have diverged, and have 1 and 1 different commits each, respectively. (use "git pull" to merge the remote branch into yours) Changes to be committed: modified: .gitignore deleted: app/controller/test/middleware/home.php deleted: app/controller/test/middleware/postprocess.php deleted: app/controller/test/middleware/preprocess.php modified: app/core/config/macros.php modified: app/core/config/micros.php modified: app/core/engine.php modified: app/core/http/define.php modified: app/core/http/init.php modified: app/core/lib/tools.php modified: app/view/home.php
1 parent f3f6610 commit d2865e9

File tree

11 files changed

+102
-48
lines changed

11 files changed

+102
-48
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/vendor/
2+
/app/controller/test/

app/controller/test/middleware/home.php

-3
This file was deleted.

app/controller/test/middleware/postprocess.php

-3
This file was deleted.

app/controller/test/middleware/preprocess.php

-3
This file was deleted.

app/core/config/macros.php

+2
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
const TEST = 'test/';
1616

1717
const PUB = ROOT . '/public/';
18+
19+
const URLPREFIX = '';
1820
?>

app/core/config/micros.php

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
const PHP = D . 'php';
1111
const JSON = D . 'json';
1212
const TXT = D . 'txt';
13+
const HOME = S . "home" . PHP
1314
?>

app/core/engine.php

+61-31
Original file line numberDiff line numberDiff line change
@@ -31,54 +31,84 @@
3131
$target = URN;
3232
}
3333

34-
if (strpos($target, '..') !== false) {
34+
if (strpos($target, '..') !== false || strpos($target, '.php') !== false) {
3535
http_status(404);
3636
}
3737

3838
if (empty($target) || $target == '/') {
3939
$target = '/home';
4040
}
4141

42-
if (is_dir(CONTROLLER . $target) and file_exists(CONTROLLER . $target . '/home.php')) {
43-
$target .= '/home';
44-
}
45-
46-
4742
$levels = explode('/', trim($target, "/"));
48-
$controller = CONTROLLER . $target . '.php';
43+
$link_vars = [];
44+
$middlewares = [];
45+
$level_path = CONTROLLER;
46+
foreach ($levels as $level) {
47+
if (in_array($level, ["_cli", "middleware", "preprocess", "postprocess", "dynamic"])) {
48+
http_status(404);
49+
}
4950

50-
if ($levels[0] == '_cli' and !CLI) {
51-
http_status(404);
52-
}
51+
$candid_level_path = $level_path . "/" . $level;
52+
$dynamic_folder = $level_path . '/dynamic';
53+
$dynamic_file = $level_path . '/dynamic.php';
5354

54-
if (in_array($levels[count($levels) - 1], ["middleware", "preprocess", "postprocess"])) {
55-
http_status(404);
56-
}
55+
if ($level === end($levels)) {
56+
if (file_exists($candid_level_path . HOME)) {
57+
$level_path .= "/" . $level . HOME;
58+
}
59+
else if (file_exists($candid_level_path . PHP)) {
60+
$level_path .= "/" . $level . PHP;
61+
}
62+
else if (file_exists($dynamic_folder . HOME)) {
63+
$link_vars[] = $level;
64+
$level_path = $dynamic_folder . HOME;
65+
}
66+
else if (file_exists($dynamic_file)) {
67+
$link_vars[] = $level;
68+
$level_path = $dynamic_file;
69+
}
70+
else {
71+
http_status(404);
72+
}
73+
}
74+
else {
75+
if (is_dir($candid_level_path)) {
76+
$level_path .= "/" . $level;
77+
}
78+
else if (is_dir($dynamic_folder)) {
79+
$link_vars[] = $level;
80+
$level_path = $dynamic_folder;
81+
}
82+
else {
83+
http_status(404);
84+
}
85+
86+
$middleware_file = $level_path . '/middleware.php';
5787

58-
if (file_exists($controller)) {
59-
$level_path = "";
60-
foreach ($levels as $level) {
61-
$level_path .= "/" . $level;
62-
$middleware_file = CONTROLLER . $level_path . '/middleware.php';
6388
if (file_exists($middleware_file)) {
64-
require $middleware_file;
89+
$middlewares[] = $middleware_file;
6590
}
6691
}
6792

68-
$preprocess_file = dirname($controller) . '/preprocess.php';
69-
if (file_exists($preprocess_file)) {
70-
require $preprocess_file;
71-
}
93+
94+
}
7295

73-
require $controller;
96+
$controller = $level_path;
7497

75-
$postprocess_file = dirname($controller) . '/postprocess.php';
76-
if (file_exists($postprocess_file)) {
77-
require $postprocess_file;
78-
}
79-
}
80-
else {
81-
http_status(404);
98+
foreach ($middlewares as $middleware) {
99+
require $middleware;
100+
}
101+
102+
$preprocess_file = dirname($controller) . '/preprocess.php';
103+
if (file_exists($preprocess_file)) {
104+
require $preprocess_file;
105+
}
106+
107+
require $controller;
108+
109+
$postprocess_file = dirname($controller) . '/postprocess.php';
110+
if (file_exists($postprocess_file)) {
111+
require $postprocess_file;
82112
}
83113

84114
die_gracefully();

app/core/http/define.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
define('PROTOCOL', 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://');
77
define('REQURI', $_SERVER['REQUEST_URI']);
88
define('REQER', REQURI);
9-
define('URN', explode("?", REQURI)[0]);
9+
define('URN', explode("?", preg_replace('/^' . preg_quote(URLPREFIX, '/') . '/', '', REQURI))[0]);
1010
define('URL', PROTOCOL . REQUEST . REQURI);
1111
}

app/core/http/init.php

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<?php
2-
ob_start();
3-
42
if (DEBUG) {
53
error_reporting(E_ALL);
64
ini_set('display_errors', true);

app/core/lib/tools.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ function view($view, $data = [], $options = []){
4444
}
4545
}
4646
function render($view, $data = [], $options = []){
47+
ob_start();
4748
view($view, $data, $options);
4849
return ob_get_clean();
4950
}
5051
function close_everything() {
51-
ob_end_flush();
5252
session_write_close();
5353
}
5454
function die_gracefully() {
@@ -72,7 +72,6 @@ function cout($data, $delimiter = N) {
7272
echo $delimiter;
7373
}
7474
function response($data, $meta = []) {
75-
ob_clean();
7675
if(is_callable($data)) {
7776
echo $data();
7877
}
@@ -101,7 +100,6 @@ function fail($data = [], $status = "FAILED") {
101100
status($status, $data);
102101
}
103102
function http_status($code, $data = [], $meta = []) {
104-
ob_clean();
105103
if (CLI) {
106104
cout("Error: " . $code, N);
107105
}

app/view/home.php

+35-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,42 @@
4949
<li>Controller <code>controller/user/home.php</code> will be executed.</li>
5050
<li>Postprocess <code>controller/user/postprocess.php</code> will be executed.</li>
5151
</ol>
52+
<h3>Dynamic routing</h3>
53+
<p>Dynamic routing in the AIS Framework allows for flexible URL patterns that adapt to the incoming requests dynamically. Instead of having a fixed URL structure, dynamic routing uses patterns that can match multiple URL paths. Here's how it works:</p>
54+
<pre>
55+
---- app
56+
-------- controller
57+
------------ user
58+
---------------- dynamic.php (Accessible via /user/{value})
59+
</pre>
60+
<p>For example, if a request is made to <code>/user/123</code>, the dynamic routing system will interpret <code>123</code> as a parameter and process it using the <code>user/dynamic.php</code> controller by <code>$link_vars</code> variable. </p>
61+
<pre>
62+
&lt;?php
63+
// controller/user/dynamic.php
64+
echo "User id: " . $link_vars[0]; // User id: 123
65+
?&gt;
66+
</pre>
67+
<h3>Multiple dynamic routing</h3>
68+
<p>You can use directory structure for dynamic routing too, For example:</p>
69+
<pre>
70+
---- app
71+
-------- controller
72+
------------ user
73+
---------------- dynamic
74+
-------------------- dynamic
75+
------------------------ home.php (Accessible via /user/{value[0]}/{value[1]})
76+
------------------------ open.php (Accessible via /user/{value[0]}/{value[1]}/open)
77+
-------------------- home.php (Accessible via /user/{value[0]})
78+
-------------------- edit.php (Accessible via /user/{value[0]}/edit)
79+
</pre>
80+
<p>For example, if a request is made to <code>/user/123/AIS-Project</code>, the dynamic routing system will interpret <code>123</code> as a parameter and process it using the <code>user/dynamic.php</code> controller by <code>$link_vars</code> variable. </p>
81+
<pre>
82+
&lt;?php
83+
// controller/user/dynamic/dynamic/open.php
84+
echo "Opening: " . $link_vars[1] . " of " . $link_vars[1] . " user."; // Opening AIS-Project of 123 user.
85+
?&gt;
86+
</pre>
5287
<hr>
53-
54-
5588
<h2>Views</h2>
5689
<p>You can load a view using the <code>view(view_path, data, options)</code> function.</p>
5790
<pre>

0 commit comments

Comments
 (0)