PostsController.php 6.0 KB

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