Skip to content

Commit

Permalink
Add RESTful examples
Browse files Browse the repository at this point in the history
  • Loading branch information
riverside committed Sep 23, 2024
1 parent 6b97821 commit dfce38d
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/07-restful/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_path_=$1 [QSA,L]
</IfModule>
87 changes: 87 additions & 0 deletions examples/07-restful/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
include __DIR__ . '/../../src/autoload.php';

$app = new \Riverside\Express\Application();

$testVerbs = function(\Riverside\Express\Request $req, \Riverside\Express\Response $res) {
if ($req->method == 'HEAD')
{
$res->end();
}
$res->json([
'method' => $req->method,
'content-type' => $req->get("content-type"),
'params' => $req->params,
'body' => $req->body,
]);
};

$app->get('/', function($req, $res) {
$html = <<<EEE
<form action="" method="post">
<input type="hidden" name="foo" value="bar">
<button type="submit" name="method" value="get">GET</button>
<button type="submit" name="method" value="head">HEAD</button>
<button type="submit" name="method" value="post">POST</button>
<button type="submit" name="method" value="put">PUT</button>
<button type="submit" name="method" value="patch">PATCH</button>
<button type="submit" name="method" value="delete">DELETE</button>
</form>
<code id="result"></code>
<script>
let method;
const result = document.querySelector("#result");
function handleFormSubmit(event) {
event.preventDefault();
const options = {
method: method,
headers: {
"accept": "application/json"
}
};
if (["post", "put", "patch"].includes(method)) {
options.headers["content-type"] = "application/x-www-form-urlencoded";
options.body = new URLSearchParams(new FormData(this)).toString();
} else if (method === "head") {
options.headers.accept = "*/*";
}
fetch("contact/1", options)
.then(function(response) {
return response.ok ? (method === "head" ? response.headers : response.json()) : Promise.reject(response);
})
.then(function(data) {
if (method === "head") {
const tmp = {};
for (let [key, value] of data.entries()) {
tmp[key] = value;
}
data = tmp;
}
result.innerText = JSON.stringify(data, null, 4);
})
.catch(function(reason) {
console.warn(reason);
});
}
function handleButtonClick(event) {
method = this.value;
}
[].forEach.call(document.querySelectorAll("button[type='submit']"), function(btn) {
btn.addEventListener("click", handleButtonClick);
});
document.querySelector("form").addEventListener("submit", handleFormSubmit);
</script>
EEE;
$res->send($html);
});
$app->get('contact/:id', $testVerbs);
$app->head('contact/:id', $testVerbs);
$app->post('contact/:id', $testVerbs);
$app->put('contact/:id', $testVerbs);
$app->patch('contact/:id', $testVerbs);
$app->delete('contact/:id', $testVerbs);

$app->run();
1 change: 1 addition & 0 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<li><a href="04-router/">Router</a></li>
<li><a href="05-post/">POST method</a></li>
<li><a href="06-params/">Params</a></li>
<li><a href="07-restful/">RESTful</a></li>
</ul>
</body>
Expand Down

0 comments on commit dfce38d

Please sign in to comment.