PostsController.php 4.2 KB

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