CookieComponentTestController.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.1.6
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace TestApp\Controller;
  15. use Cake\Controller\Controller;
  16. /**
  17. * CookieComponentTestController class
  18. */
  19. class CookieComponentTestController extends Controller
  20. {
  21. /**
  22. * @var array
  23. */
  24. public $components = [
  25. 'Cookie',
  26. ];
  27. public $autoRender = false;
  28. /**
  29. * view
  30. *
  31. * @param string|null $key Encryption key used. By defaults,
  32. * CookieComponent::_config['key'].
  33. */
  34. public function view($key = null)
  35. {
  36. if (isset($key)) {
  37. $this->Cookie->setConfig('key', $key);
  38. }
  39. $this->set('ValueFromRequest', $this->request->getCookie('NameOfCookie'));
  40. $this->set('ValueFromCookieComponent', $this->Cookie->read('NameOfCookie'));
  41. }
  42. /**
  43. * action to set a cookie
  44. *
  45. * @param string|null $key Encryption key used. By defaults,
  46. * CookieComponent::_config['key'].
  47. */
  48. public function set_cookie($key = null)
  49. {
  50. if (isset($key)) {
  51. $this->Cookie->setConfig('key', $key);
  52. }
  53. $this->Cookie->write('NameOfCookie', 'abc');
  54. }
  55. public function remove_cookie($key)
  56. {
  57. $this->Cookie->delete($key);
  58. }
  59. }