PostsController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. 'Security',
  31. ];
  32. /**
  33. * beforeFilter
  34. *
  35. * @return void
  36. */
  37. public function beforeFilter(Event $event)
  38. {
  39. if ($this->request->param('action') !== 'securePost') {
  40. $this->getEventManager()->off($this->Security);
  41. }
  42. }
  43. /**
  44. * Index method.
  45. *
  46. * @param string $layout
  47. * @return void
  48. */
  49. public function index($layout = 'default')
  50. {
  51. $this->Flash->error('An error message');
  52. $this->response->cookie([
  53. 'name' => 'remember_me',
  54. 'value' => 1
  55. ]);
  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\Network\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. * Post endpoint for integration testing with security component.
  80. *
  81. * @return void
  82. */
  83. public function securePost()
  84. {
  85. $this->response->body('Request was accepted');
  86. return $this->response;
  87. }
  88. public function file()
  89. {
  90. $this->response->file(__FILE__);
  91. return $this->response;
  92. }
  93. }