CookieComponentTestController.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.1.6
  12. * @license http://www.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->config('key', $key);
  38. }
  39. $this->set('ValueFromRequest', $this->request->cookie('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. $this->autoRender = false;
  51. if (isset($key)) {
  52. $this->Cookie->config('key', $key);
  53. }
  54. $this->Cookie->write('NameOfCookie', 'abc');
  55. }
  56. }