Skip to content

Commit dfce38d

Browse files
committed
Add RESTful examples
1 parent 6b97821 commit dfce38d

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

examples/07-restful/.htaccess

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<IfModule mod_rewrite.c>
2+
RewriteEngine On
3+
4+
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
5+
RewriteRule .* - [F]
6+
7+
RewriteCond %{ENV:REDIRECT_STATUS} 200
8+
RewriteRule .* - [L]
9+
10+
RewriteCond %{REQUEST_FILENAME} !-f
11+
RewriteCond %{REQUEST_FILENAME} !-d
12+
RewriteRule ^(.*)$ index.php?_path_=$1 [QSA,L]
13+
</IfModule>

examples/07-restful/index.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
include __DIR__ . '/../../src/autoload.php';
3+
4+
$app = new \Riverside\Express\Application();
5+
6+
$testVerbs = function(\Riverside\Express\Request $req, \Riverside\Express\Response $res) {
7+
if ($req->method == 'HEAD')
8+
{
9+
$res->end();
10+
}
11+
$res->json([
12+
'method' => $req->method,
13+
'content-type' => $req->get("content-type"),
14+
'params' => $req->params,
15+
'body' => $req->body,
16+
]);
17+
};
18+
19+
$app->get('/', function($req, $res) {
20+
$html = <<<EEE
21+
<form action="" method="post">
22+
<input type="hidden" name="foo" value="bar">
23+
<button type="submit" name="method" value="get">GET</button>
24+
<button type="submit" name="method" value="head">HEAD</button>
25+
<button type="submit" name="method" value="post">POST</button>
26+
<button type="submit" name="method" value="put">PUT</button>
27+
<button type="submit" name="method" value="patch">PATCH</button>
28+
<button type="submit" name="method" value="delete">DELETE</button>
29+
</form>
30+
<code id="result"></code>
31+
<script>
32+
let method;
33+
const result = document.querySelector("#result");
34+
function handleFormSubmit(event) {
35+
event.preventDefault();
36+
37+
const options = {
38+
method: method,
39+
headers: {
40+
"accept": "application/json"
41+
}
42+
};
43+
if (["post", "put", "patch"].includes(method)) {
44+
options.headers["content-type"] = "application/x-www-form-urlencoded";
45+
options.body = new URLSearchParams(new FormData(this)).toString();
46+
} else if (method === "head") {
47+
options.headers.accept = "*/*";
48+
}
49+
fetch("contact/1", options)
50+
.then(function(response) {
51+
return response.ok ? (method === "head" ? response.headers : response.json()) : Promise.reject(response);
52+
})
53+
.then(function(data) {
54+
if (method === "head") {
55+
const tmp = {};
56+
for (let [key, value] of data.entries()) {
57+
tmp[key] = value;
58+
}
59+
data = tmp;
60+
}
61+
result.innerText = JSON.stringify(data, null, 4);
62+
})
63+
.catch(function(reason) {
64+
console.warn(reason);
65+
});
66+
}
67+
68+
function handleButtonClick(event) {
69+
method = this.value;
70+
}
71+
72+
[].forEach.call(document.querySelectorAll("button[type='submit']"), function(btn) {
73+
btn.addEventListener("click", handleButtonClick);
74+
});
75+
document.querySelector("form").addEventListener("submit", handleFormSubmit);
76+
</script>
77+
EEE;
78+
$res->send($html);
79+
});
80+
$app->get('contact/:id', $testVerbs);
81+
$app->head('contact/:id', $testVerbs);
82+
$app->post('contact/:id', $testVerbs);
83+
$app->put('contact/:id', $testVerbs);
84+
$app->patch('contact/:id', $testVerbs);
85+
$app->delete('contact/:id', $testVerbs);
86+
87+
$app->run();

examples/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<li><a href="04-router/">Router</a></li>
1515
<li><a href="05-post/">POST method</a></li>
1616
<li><a href="06-params/">Params</a></li>
17+
<li><a href="07-restful/">RESTful</a></li>
1718
</ul>
1819
1920
</body>

0 commit comments

Comments
 (0)