|
| 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(); |
0 commit comments