Raw.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. use Yaf\Controller_Abstract as AbstractController;
  3. class RawController extends AbstractController
  4. {
  5. public function jsonAction ()
  6. {
  7. header('Content-type: application/json');
  8. die(json_encode(array('message' => 'Hello, World!')));
  9. }
  10. public function dbAction ()
  11. {
  12. $dbh = DatabaseManager::getInstance()->getConnection();
  13. $query_count = (int) $this->getRequest()->get('queries', 1);
  14. if (0 >= $query_count) {
  15. $query_count = 1;
  16. } elseif (500 < $query_count) {
  17. $query_count = 500;
  18. }
  19. $arr = array();
  20. $id = mt_rand(1, 10000);
  21. $statement = $dbh->prepare('SELECT `randomNumber` FROM `World` WHERE `id` = :id');
  22. $statement->bindParam(':id', $id, \PDO::PARAM_INT);
  23. while (0 < $query_count--) {
  24. $statement->execute();
  25. $arr[] = array('id' => $id, 'randomNumber' => $statement->fetchColumn());
  26. $id = mt_rand(1, 10000);
  27. }
  28. if (count($arr) == 1) {
  29. $arr = $arr[0];
  30. }
  31. header('Content-type: application/json');
  32. die(json_encode($arr));
  33. }
  34. public function fortunesAction ()
  35. {
  36. $view = $this->getView();
  37. $dbh = DatabaseManager::getInstance()->getConnection();
  38. $statement = $dbh->query('SELECT `id`, `message` FROM `Fortune`');
  39. $arr = $statement->fetchAll(\PDO::FETCH_KEY_PAIR);
  40. $arr[0] = 'Additional fortune added at request time.';
  41. asort($arr);
  42. $view->rows = $arr;
  43. header('Content-Type: text/html; charset=utf-8');
  44. }
  45. }