SiteController.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // Create an array with the response string.
  25. $arr = array();
  26. // For each query, store the result set values in the response array
  27. while (0 < $queries--) {
  28. // Store result in array.
  29. $arr[] = $statement->bindValue(':id',mt_rand(1, 10000))->queryOne();
  30. }
  31. }
  32. return $this->resJson($arr);
  33. }
  34. public function actionFortunes() {
  35. $arr = Yii::$app->db->createCommand('select id, message from Fortune')->queryAll();
  36. $arr[0] = 'Additional fortune added at request time.';
  37. asort($arr);
  38. header("Content-Type: text/html; charset=utf-8");
  39. echo <<<EOM
  40. <!DOCTYPE html>
  41. <html>
  42. <head>
  43. <title>Fortunes</title>
  44. </head>
  45. <body>
  46. <table>
  47. <tr>
  48. <th>id</th>
  49. <th>message</th>
  50. </tr>
  51. EOM;
  52. foreach ( $arr as $id => $fortune ) {
  53. echo '<tr>';
  54. echo '<td>'.$id.'</td>';
  55. echo '<td>'.htmlspecialchars($fortune, ENT_QUOTES, 'utf-8').'</td>';
  56. echo '</tr>';
  57. }
  58. echo <<<EOM
  59. </table>
  60. </body>
  61. </html>
  62. EOM;
  63. }
  64. public function actionUpdates($queries = 1) {
  65. if ($queries > 500) $queries = 500;
  66. elseif ($queries < 0 ) $queries = 1;
  67. $selectCommand = Yii::$app->db->createCommand('select randomNumber from World where id = :id');
  68. $updateCommand = Yii::$app->db->createCommand('update World set randomNumber = :num where id = :id');
  69. $arr = [];
  70. while (0 < $queries--) {
  71. // Store result in array.
  72. $id = mt_rand(1,10000);
  73. $randomNumber = mt_rand(1, 1000);
  74. $selectCommand->bindParam(':id',$id)->queryScalar();
  75. $updateCommand->bindValues([':id'=>$id,':num'=>$randomNumber])->execute();
  76. $arr[] = array('id' => $id, 'randomNumber' => $randomNumber);
  77. }
  78. return $this->resJson($arr);
  79. }
  80. public function actionPlaintext() {
  81. header("Content-Type: text/plain;");
  82. echo 'Hello, World!';
  83. }
  84. }