PostsController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace TestApp\Controller;
  17. use Cake\Event\EventInterface;
  18. use Cake\Http\Cookie\Cookie;
  19. /**
  20. * PostsController class
  21. */
  22. class PostsController extends AppController
  23. {
  24. public function initialize(): void
  25. {
  26. $this->loadComponent('Flash');
  27. $this->loadComponent('RequestHandler');
  28. $this->loadComponent('Security');
  29. }
  30. /**
  31. * beforeFilter
  32. *
  33. * @return \Cake\Http\Response|null|void
  34. */
  35. public function beforeFilter(EventInterface $event)
  36. {
  37. if ($this->request->getParam('action') !== 'securePost') {
  38. $this->getEventManager()->off($this->Security);
  39. }
  40. }
  41. /**
  42. * Index method.
  43. *
  44. * @param string $layout
  45. * @return void
  46. */
  47. public function index($layout = 'default')
  48. {
  49. $this->Flash->error('An error message');
  50. $this->response = $this->response->withCookie(new Cookie('remember_me', 1));
  51. $this->set('test', 'value');
  52. $this->viewBuilder()->setLayout($layout);
  53. }
  54. /**
  55. * Sets a flash message and redirects (no rendering)
  56. *
  57. * @return \Cake\Http\Response
  58. */
  59. public function flashNoRender()
  60. {
  61. $this->Flash->error('An error message');
  62. return $this->redirect(['action' => 'index']);
  63. }
  64. /**
  65. * Stub get method
  66. *
  67. * @return void
  68. */
  69. public function get()
  70. {
  71. // Do nothing.
  72. }
  73. /**
  74. * Stub AJAX method
  75. *
  76. * @return void
  77. */
  78. public function ajax()
  79. {
  80. $data = [];
  81. $this->set(compact('data'));
  82. $this->viewBuilder()->setOption('serialize', ['data']);
  83. }
  84. /**
  85. * Post endpoint for integration testing with security component.
  86. *
  87. * @return void
  88. */
  89. public function securePost()
  90. {
  91. return $this->response->withStringBody('Request was accepted');
  92. }
  93. public function file()
  94. {
  95. return $this->response->withFile(__FILE__);
  96. }
  97. public function header()
  98. {
  99. return $this->getResponse()->withHeader('X-Cake', 'custom header');
  100. }
  101. public function hostData()
  102. {
  103. $data = [
  104. 'host' => $this->request->host(),
  105. 'isSsl' => $this->request->is('ssl'),
  106. ];
  107. return $this->getResponse()->withStringBody(json_encode($data));
  108. }
  109. public function empty_response()
  110. {
  111. return $this->getResponse()->withStringBody('');
  112. }
  113. public function secretCookie()
  114. {
  115. return $this->response
  116. ->withCookie(new Cookie('secrets', 'name'))
  117. ->withStringBody('ok');
  118. }
  119. public function stacked_flash()
  120. {
  121. $this->Flash->error('Error 1');
  122. $this->Flash->error('Error 2');
  123. $this->Flash->success('Success 1', ['key' => 'custom']);
  124. $this->Flash->success('Success 2', ['key' => 'custom']);
  125. return $this->getResponse()->withStringBody('');
  126. }
  127. public function throw_exception()
  128. {
  129. $this->Flash->error('Error 1');
  130. throw new \OutOfBoundsException('oh no!');
  131. }
  132. }