Skip to content

Commit 295bfa8

Browse files
committed
feat: cache and $res
1 parent 117c995 commit 295bfa8

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ Minimalist web framework for [PHP](https://www.php.net/)
1111
require 'phpExpress.php';
1212
$app = new phpExpress();
1313

14-
$app->get('/', function ($req) {
15-
return "Hello World";
14+
$app->get('/', function ($req, $res) {
15+
$res->send("Hello World");
1616
});
17-
17+
1818
$app->listen();
1919
```

lib/phpExpress.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public function __construct()
1717
ob_start('ob_gzhandler'); //gzip
1818
header_remove('X-Powered-By'); // server type remove
1919
header_remove('Host'); // (https://webhint.io/docs/user-guide/hints/hint-no-disallowed-headers/?source=devtools)
20-
header("Cache-Control: no-cache"); // (https://webhint.io/docs/user-guide/hints/hint-http-cache/?source=devtools)
2120
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
2221
$url = "https://";
2322
} else {
@@ -32,8 +31,14 @@ public function __construct()
3231
$this->req = json_decode(file_get_contents('php://input'), true);
3332
}
3433

35-
public static function send($result)
34+
public static function send($result, $cache = false)
3635
{
36+
if ($cache) {
37+
header("Cache-Control: max-age=3600"); // (https://webhint.io/docs/user-guide/hints/hint-http-cache/?source=devtools)
38+
} else {
39+
header("Cache-Control: no-cache");
40+
}
41+
3742
if (is_object($result)) { // JSON
3843
header('content-type: application/json; charset=utf-8');
3944
$len = strlen(json_encode($result));
@@ -109,7 +114,7 @@ public function get($parm, $function)
109114
}
110115

111116
http_response_code(200);
112-
$this->send($function($this->req));
117+
$function($this->req, $this);
113118
}
114119

115120
public function post($parm, $function)

test/get.test.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
// Request : http://localhost:3000/test/get.test.php/3/get
77
// Result : 3
8-
$app->get('/{id}/get', static function ($req) {
8+
$app->get('/{id}/get', function ($req, $res) {
99
try {
10-
return $req["id"];
10+
$res->send($req["id"]);
1111
} catch (Exception $e) {
1212
http_response_code(500);
1313
return $e;
@@ -16,9 +16,9 @@
1616

1717
// Request : http://localhost:3000/test/get.test.php/test
1818
// Result : phpExpress
19-
$app->get('/test', static function ($req) {
19+
$app->get('/test', function ($req, $res) {
2020
try {
21-
return "phpExpress";
21+
$res->send("phpExpress");
2222
} catch (Exception $e) {
2323
http_response_code(500);
2424
return $e;
@@ -27,9 +27,9 @@
2727

2828
// Request : http://localhost:3000/test/get.test.php/test2
2929
// Result : {"msg":"phpExpress"}
30-
$app->get('/test2', static function ($req) {
30+
$app->get('/test2', function ($req, $res) {
3131
try {
32-
return (object) ["msg" => "phpExpress"];
32+
$res->send((object) ["msg" => "phpExpress"]);
3333
} catch (Exception $e) {
3434
http_response_code(500);
3535
return $e;

0 commit comments

Comments
 (0)