SiteController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\web\Controller;
  5. class SiteController extends Controller
  6. {
  7. private function resJson($data) {
  8. header('Content-type: application/json');
  9. echo json_encode($data);
  10. }
  11. public function actionJson() {
  12. return $this->resJson(array('message'=>'Hello, World!'));
  13. }
  14. public function actionDb($queries = 1) {
  15. // Set up for Test
  16. // $cmd = Yii::$app->db->createCommand('insert into World (randomNumber) values (:v)');
  17. // for($i = 1; $i <=10000 ; $i ++ ) {
  18. // $cmd->bindValue(':v',mt_rand(1, 10000))->execute();
  19. // }
  20. $statement = Yii::$app->db->createCommand('select id,randomNumber from World where id = :id');
  21. if ($queries == 1) {
  22. $arr = $statement->bindValue(':id',mt_rand(1, 10000))->queryOne();
  23. } else {
  24. if ($queries > 500) $queries = 500;
  25. elseif ($queries < 0 ) $queries = 1;
  26. // Create an array with the response string.
  27. $arr = array();
  28. // For each query, store the result set values in the response array
  29. while (0 < $queries--) {
  30. // Store result in array.
  31. $arr[] = $statement->bindValue(':id',mt_rand(1, 10000))->queryOne();
  32. }
  33. }
  34. return $this->resJson($arr);
  35. }
  36. public function actionFortunes() {
  37. $arr = Yii::$app->db->createCommand('select id, message from Fortune')->queryAll();
  38. $arr[0] = 'Additional fortune added at request time.';
  39. asort($arr);
  40. header("Content-Type: text/html; charset=utf-8");
  41. echo <<<EOM
  42. <!DOCTYPE html>
  43. <html>
  44. <head>
  45. <title>Fortunes</title>
  46. </head>
  47. <body>
  48. <table>
  49. <tr>
  50. <th>id</th>
  51. <th>message</th>
  52. </tr>
  53. EOM;
  54. foreach ( $arr as $id => $fortune ) {
  55. echo '<tr>';
  56. echo '<td>'.$id.'</td>';
  57. echo '<td>'.htmlspecialchars($fortune, ENT_QUOTES, 'utf-8').'</td>';
  58. echo '</tr>';
  59. }
  60. echo <<<EOM
  61. </table>
  62. </body>
  63. </html>
  64. EOM;
  65. }
  66. public function actionUpdates($queries = 1) {
  67. if ($queries > 500) $queries = 500;
  68. elseif ($queries < 0 ) $queries = 1;
  69. $selectCommand = Yii::$app->db->createCommand('select randomNumber from World where id = :id');
  70. $updateCommand = Yii::$app->db->createCommand('update World set randomNumber = :num where id = :id');
  71. $arr = [];
  72. while (0 < $queries--) {
  73. // Store result in array.
  74. $id = mt_rand(1,10000);
  75. $randomNumber = mt_rand(1, 1000);
  76. $selectCommand->bindParam(':id',$id)->queryScalar();
  77. $updateCommand->bindValues([':id'=>$id,':num'=>$randomNumber])->execute();
  78. $arr[] = array('id' => $id, 'randomNumber' => $randomNumber);
  79. }
  80. return $this->resJson($arr);
  81. }
  82. public function actionPlaintext() {
  83. header("Content-Type: text/plain;");
  84. echo 'Hello, World!';
  85. }
  86. }