response->header('Content-type', 'application/json; charset=UTF-8'); return json_encode($data); } public function toJson() { return $this->json(['message' => 'Hello, World!']); } public function plaintext() { $this->response->header('Content-Type', 'text/plain'); return 'Hello, World!'; } public function db() { return $this->json(World::repeatStatement()->find(mt_rand(1, 10000))); } public function fortunes() { $data = Fortune::findAll()->jsonSerialize(); $data[] = (object)['id' => 0, 'message' => 'Additional fortune added at request time.']; usort($data, function ($a, $b) { return $a->message <=> $b->message; }); $html = ''; foreach ($data as $f) { $f->message = htmlspecialchars($f->message, ENT_QUOTES, 'UTF-8'); $html .= "
id | message |
---|