FlashHelper.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View\Helper;
  16. use Cake\View\Helper;
  17. /**
  18. * FlashHelper class to render flash messages.
  19. *
  20. * After setting messsages in your controllers with FlashComponent, you can use
  21. * this class to output your flash messages in your views.
  22. */
  23. class FlashHelper extends Helper {
  24. /**
  25. * Used to render the message set in FlashComponent::set()
  26. *
  27. * In your view: $this->Flash->render('somekey');
  28. * Will default to flash if no param is passed
  29. *
  30. * You can pass additional information into the flash message generation. This allows you
  31. * to consolidate all the parameters for a given type of flash message into the view.
  32. *
  33. * {{{
  34. * echo $this->Flash->render('flash', ['params' => ['name' => $user['User']['name']]]);
  35. * }}}
  36. *
  37. * This would pass the current user's name into the flash message, so you could create personalized
  38. * messages without the controller needing access to that data.
  39. *
  40. * Lastly you can choose the element that is used for rendering the flash message. Using
  41. * custom elements allows you to fully customize how flash messages are generated.
  42. *
  43. * {{{
  44. * echo $this->Flash->render('flash', [element' => 'my_custom_element']);
  45. * }}}
  46. *
  47. * If you want to use an element from a plugin for rendering your flash message
  48. * you can use the dot notation for the plugin's element name:
  49. *
  50. * {{{
  51. * echo $this->Flash->render('flash', [
  52. * 'element' => 'MyPlugin.my_custom_element',
  53. * ]);
  54. * }}}
  55. *
  56. * @param string $key The [Flash.]key you are rendering in the view.
  57. * @param array $options Additional options to use for the creation of this flash message.
  58. * Supports the 'params', and 'element' keys that are used in the helper.
  59. * @return string
  60. */
  61. public function render($key = 'flash', array $options = []) {
  62. if (!$this->request->session()->check("Flash.$key")) {
  63. return '';
  64. }
  65. $flash = $options + $this->request->session()->read("Flash.$key");
  66. $this->request->session()->delete("Flash.$key");
  67. return $this->_View->element($flash['element'], $flash);
  68. }
  69. /**
  70. * Event listeners.
  71. *
  72. * @return array
  73. */
  74. public function implementedEvents() {
  75. return [];
  76. }
  77. }