PostsController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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->param('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->cookie([
  55. 'name' => 'remember_me',
  56. 'value' => 1
  57. ]);
  58. $this->set('test', 'value');
  59. $this->viewBuilder()->setLayout($layout);
  60. }
  61. /**
  62. * Sets a flash message and redirects (no rendering)
  63. *
  64. * @return \Cake\Network\Response
  65. */
  66. public function flashNoRender()
  67. {
  68. $this->Flash->error('An error message');
  69. return $this->redirect(['action' => 'index']);
  70. }
  71. /**
  72. * Stub get method
  73. *
  74. * @return void
  75. */
  76. public function get()
  77. {
  78. // Do nothing.
  79. }
  80. /**
  81. * Post endpoint for integration testing with security component.
  82. *
  83. * @return void
  84. */
  85. public function securePost()
  86. {
  87. $this->response->body('Request was accepted');
  88. return $this->response;
  89. }
  90. public function file()
  91. {
  92. $this->response->file(__FILE__);
  93. return $this->response;
  94. }
  95. }