PostsController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace TestApp\Controller;
  16. use Cake\Event\Event;
  17. use Cake\Http\Cookie\Cookie;
  18. /**
  19. * PostsController class
  20. */
  21. class PostsController extends AppController
  22. {
  23. /**
  24. * Components array
  25. *
  26. * @var array
  27. */
  28. public $components = [
  29. 'Flash',
  30. 'RequestHandler' => [
  31. 'enableBeforeRedirect' => false
  32. ],
  33. 'Security',
  34. ];
  35. /**
  36. * beforeFilter
  37. *
  38. * @return void
  39. */
  40. public function beforeFilter(Event $event)
  41. {
  42. if ($this->request->getParam('action') !== 'securePost') {
  43. $this->getEventManager()->off($this->Security);
  44. }
  45. }
  46. /**
  47. * Index method.
  48. *
  49. * @param string $layout
  50. * @return void
  51. */
  52. public function index($layout = 'default')
  53. {
  54. $this->Flash->error('An error message');
  55. $this->response = $this->response->withCookie(new Cookie('remember_me', 1));
  56. $this->set('test', 'value');
  57. $this->viewBuilder()->setLayout($layout);
  58. }
  59. /**
  60. * Sets a flash message and redirects (no rendering)
  61. *
  62. * @return \Cake\Http\Response
  63. */
  64. public function flashNoRender()
  65. {
  66. $this->Flash->error('An error message');
  67. return $this->redirect(['action' => 'index']);
  68. }
  69. /**
  70. * Stub get method
  71. *
  72. * @return void
  73. */
  74. public function get()
  75. {
  76. // Do nothing.
  77. }
  78. /**
  79. * Stub AJAX method
  80. *
  81. * @return void
  82. */
  83. public function ajax()
  84. {
  85. $data = [];
  86. $this->set(compact('data'));
  87. $this->set('_serialize', ['data']);
  88. }
  89. /**
  90. * Post endpoint for integration testing with security component.
  91. *
  92. * @return void
  93. */
  94. public function securePost()
  95. {
  96. return $this->response->withStringBody('Request was accepted');
  97. }
  98. public function file()
  99. {
  100. return $this->response->withFile(__FILE__);
  101. }
  102. public function header()
  103. {
  104. return $this->getResponse()->withHeader('X-Cake', 'custom header');
  105. }
  106. public function empty_response()
  107. {
  108. return $this->getResponse()->withStringBody('');
  109. }
  110. public function stacked_flash()
  111. {
  112. $this->Flash->error('Error 1');
  113. $this->Flash->error('Error 2');
  114. $this->Flash->success('Success 1', ['key' => 'custom']);
  115. $this->Flash->success('Success 2', ['key' => 'custom']);
  116. return $this->getResponse()->withStringBody('');
  117. }
  118. }