PostsController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. use Cake\Http\Exception\RedirectException;
  20. use Cake\Http\Response;
  21. use Cake\View\JsonView;
  22. use OutOfBoundsException;
  23. use RuntimeException;
  24. /**
  25. * PostsController class
  26. */
  27. class PostsController extends AppController
  28. {
  29. /**
  30. * @return void
  31. */
  32. public function initialize(): void
  33. {
  34. $this->loadComponent('Flash');
  35. $this->loadComponent('FormProtection');
  36. $this->middleware(function ($request, $handler) {
  37. return $handler->handle($request->withAttribute('for-all', true));
  38. });
  39. $this->middleware(function ($request, $handler) {
  40. return $handler->handle($request->withAttribute('index-only', true));
  41. }, ['only' => 'index']);
  42. $this->middleware(function ($request, $handler) {
  43. return $handler->handle($request->withAttribute('all-except-index', true));
  44. }, ['except' => ['index']]);
  45. }
  46. /**
  47. * @return \Cake\Http\Response|null|void
  48. */
  49. public function beforeFilter(EventInterface $event)
  50. {
  51. if ($this->request->getParam('action') !== 'securePost') {
  52. $this->getEventManager()->off($this->FormProtection);
  53. }
  54. $this->FormProtection->setConfig('unlockedFields', ['some_unlocked_field']);
  55. }
  56. public function beforeRender(EventInterface $event)
  57. {
  58. if ($this->request->getQuery('clear')) {
  59. $this->set('flash', $this->request->getSession()->consume('Flash'));
  60. }
  61. }
  62. public function viewClasses(): array
  63. {
  64. return [JsonView::class];
  65. }
  66. /**
  67. * Index method.
  68. *
  69. * @param string $layout
  70. * @return void
  71. */
  72. public function index($layout = 'default')
  73. {
  74. $this->Flash->error('An error message');
  75. $this->response = $this->response->withCookie(new Cookie('remember_me', 1));
  76. $this->set('test', 'value');
  77. $this->viewBuilder()->setLayout($layout);
  78. }
  79. /**
  80. * @return \Cake\Http\Response|null
  81. */
  82. public function someRedirect(): ?Response
  83. {
  84. $this->Flash->success('A success message');
  85. return $this->redirect('/somewhere');
  86. }
  87. /**
  88. * Sets a flash message and redirects (no rendering)
  89. *
  90. * @return \Cake\Http\Response
  91. */
  92. public function flashNoRender(): ?Response
  93. {
  94. $this->Flash->error('An error message');
  95. return $this->redirect(['action' => 'index']);
  96. }
  97. /**
  98. * Stub get method
  99. *
  100. * @return void
  101. */
  102. public function get()
  103. {
  104. // Do nothing.
  105. }
  106. /**
  107. * Stub AJAX method
  108. *
  109. * @return void
  110. */
  111. public function ajax()
  112. {
  113. $data = [];
  114. $this->set(compact('data'));
  115. $this->viewBuilder()->setOption('serialize', ['data']);
  116. }
  117. /**
  118. * Post endpoint for integration testing with security component.
  119. *
  120. * @return void
  121. */
  122. public function securePost()
  123. {
  124. return $this->response->withStringBody('Request was accepted');
  125. }
  126. /**
  127. * @return \Cake\Http\Response
  128. */
  129. public function file()
  130. {
  131. $filename = $this->request->getQuery('file');
  132. if ($filename) {
  133. $path = TMP . $filename;
  134. return $this->response->withFile($path, ['download' => true])
  135. ->withHeader('Content-Disposition', "attachment;filename=*UTF-8''{$filename}");
  136. }
  137. return $this->response->withFile(__FILE__);
  138. }
  139. /**
  140. * @return \Cake\Http\Response
  141. */
  142. public function header()
  143. {
  144. return $this->getResponse()->withHeader('X-Cake', 'custom header');
  145. }
  146. /**
  147. * @return \Cake\Http\Response
  148. */
  149. public function hostData()
  150. {
  151. $data = [
  152. 'host' => $this->request->host(),
  153. 'isSsl' => $this->request->is('https'),
  154. ];
  155. return $this->getResponse()->withStringBody(json_encode($data));
  156. }
  157. /**
  158. * @return \Cake\Http\Response
  159. */
  160. public function empty_response()
  161. {
  162. return $this->getResponse()->withStringBody('');
  163. }
  164. /**
  165. * @return \Cake\Http\Response
  166. */
  167. public function secretCookie()
  168. {
  169. return $this->response
  170. ->withCookie(new Cookie('secrets', 'name'))
  171. ->withStringBody('ok');
  172. }
  173. public function redirectWithCookie()
  174. {
  175. $cookies = [
  176. Cookie::create('remember', '1'),
  177. Cookie::create('expired', '')->withExpired(),
  178. ];
  179. $values = [];
  180. foreach ($cookies as $cookie) {
  181. $values[] = $cookie->toHeaderValue();
  182. }
  183. $headers = ['Set-Cookie' => $values];
  184. throw new RedirectException('/posts', 302, $headers);
  185. }
  186. /**
  187. * @return \Cake\Http\Response
  188. */
  189. public function stacked_flash()
  190. {
  191. $this->Flash->error('Error 1');
  192. $this->Flash->error('Error 2');
  193. $this->Flash->success('Success 1', ['key' => 'custom']);
  194. $this->Flash->success('Success 2', ['key' => 'custom']);
  195. return $this->getResponse()->withStringBody('');
  196. }
  197. /**
  198. * @return \Cake\Http\Response
  199. */
  200. public function throw_exception(): never
  201. {
  202. $this->Flash->error('Error 1');
  203. throw new OutOfBoundsException('oh no!');
  204. }
  205. /**
  206. * @return \Cake\Http\Response
  207. */
  208. public function throw_chained(): never
  209. {
  210. $inner = new RuntimeException('inner badness');
  211. throw new OutOfBoundsException('oh no!', 1, $inner);
  212. }
  213. }