PostsController.php 3.9 KB

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