CookieComponentTestController.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. /**
  28. * view
  29. *
  30. * @param string|null $key Encryption key used. By defaults,
  31. * CookieComponent::_config['key'].
  32. */
  33. public function view($key = null)
  34. {
  35. if (isset($key)) {
  36. $this->Cookie->config('key', $key);
  37. }
  38. $this->set('ValueFromRequest', $this->request->cookie('NameOfCookie'));
  39. $this->set('ValueFromCookieComponent', $this->Cookie->read('NameOfCookie'));
  40. }
  41. /**
  42. * action to set a cookie
  43. *
  44. * @param string|null $key Encryption key used. By defaults,
  45. * CookieComponent::_config['key'].
  46. */
  47. public function set_cookie($key = null)
  48. {
  49. $this->autoRender = false;
  50. if (isset($key)) {
  51. $this->Cookie->config('key', $key);
  52. }
  53. $this->Cookie->write('NameOfCookie', 'abc');
  54. }
  55. }