PostsController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /**
  18. * PostsController class
  19. */
  20. class PostsController extends AppController
  21. {
  22. /**
  23. * Components array
  24. *
  25. * @var array
  26. */
  27. public $components = [
  28. 'Flash',
  29. 'RequestHandler' => [
  30. 'enableBeforeRedirect' => false
  31. ],
  32. 'Security',
  33. ];
  34. /**
  35. * beforeFilter
  36. *
  37. * @return void
  38. */
  39. public function beforeFilter(Event $event)
  40. {
  41. if ($this->request->getParam('action') !== 'securePost') {
  42. $this->getEventManager()->off($this->Security);
  43. }
  44. }
  45. /**
  46. * Index method.
  47. *
  48. * @param string $layout
  49. * @return void
  50. */
  51. public function index($layout = 'default')
  52. {
  53. $this->Flash->error('An error message');
  54. $this->response = $this->response->withCookie('remember_me', 1);
  55. $this->set('test', 'value');
  56. $this->viewBuilder()->setLayout($layout);
  57. }
  58. /**
  59. * Sets a flash message and redirects (no rendering)
  60. *
  61. * @return \Cake\Http\Response
  62. */
  63. public function flashNoRender()
  64. {
  65. $this->Flash->error('An error message');
  66. return $this->redirect(['action' => 'index']);
  67. }
  68. /**
  69. * Stub get method
  70. *
  71. * @return void
  72. */
  73. public function get()
  74. {
  75. // Do nothing.
  76. }
  77. /**
  78. * Stub AJAX method
  79. *
  80. * @return void
  81. */
  82. public function ajax()
  83. {
  84. $data = [];
  85. $this->set(compact('data'));
  86. $this->set('_serialize', ['data']);
  87. }
  88. /**
  89. * Post endpoint for integration testing with security component.
  90. *
  91. * @return void
  92. */
  93. public function securePost()
  94. {
  95. return $this->response->withStringBody('Request was accepted');
  96. }
  97. public function file()
  98. {
  99. return $this->response->withFile(__FILE__);
  100. }
  101. }