PostsController.php 5.0 KB

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