/lib/Cake/Controller/Component/SessionComponent.php

https://github.com/masihnewbie/cakephp · PHP · 186 lines · 41 code · 15 blank · 130 comment · 0 complexity · 87e6be43545db0fee18aad8d63785bca MD5 · raw file

  1. <?php
  2. /**
  3. * SessionComponent. Provides access to Sessions from the Controller layer
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Controller.Component
  16. * @since CakePHP(tm) v 0.10.0.1232
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Component', 'Controller');
  20. App::uses('CakeSession', 'Model/Datasource');
  21. /**
  22. * Session Component.
  23. *
  24. * Session handling from the controller.
  25. *
  26. * @package Cake.Controller.Component
  27. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
  28. * @link http://book.cakephp.org/2.0/en/development/sessions.html
  29. */
  30. class SessionComponent extends Component {
  31. /**
  32. * Get / Set the userAgent
  33. *
  34. * @param string $userAgent Set the userAgent
  35. * @return void
  36. */
  37. public function userAgent($userAgent = null) {
  38. return CakeSession::userAgent($userAgent);
  39. }
  40. /**
  41. * Used to write a value to a session key.
  42. *
  43. * In your controller: $this->Session->write('Controller.sessKey', 'session value');
  44. *
  45. * @param string $name The name of the key your are setting in the session.
  46. * This should be in a Controller.key format for better organizing
  47. * @param string $value The value you want to store in a session.
  48. * @return boolean Success
  49. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::write
  50. */
  51. public function write($name, $value = null) {
  52. return CakeSession::write($name, $value);
  53. }
  54. /**
  55. * Used to read a session values for a key or return values for all keys.
  56. *
  57. * In your controller: $this->Session->read('Controller.sessKey');
  58. * Calling the method without a param will return all session vars
  59. *
  60. * @param string $name the name of the session key you want to read
  61. * @return mixed value from the session vars
  62. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::read
  63. */
  64. public function read($name = null) {
  65. return CakeSession::read($name);
  66. }
  67. /**
  68. * Wrapper for SessionComponent::del();
  69. *
  70. * In your controller: $this->Session->delete('Controller.sessKey');
  71. *
  72. * @param string $name the name of the session key you want to delete
  73. * @return boolean true is session variable is set and can be deleted, false is variable was not set.
  74. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::delete
  75. */
  76. public function delete($name) {
  77. return CakeSession::delete($name);
  78. }
  79. /**
  80. * Used to check if a session variable is set
  81. *
  82. * In your controller: $this->Session->check('Controller.sessKey');
  83. *
  84. * @param string $name the name of the session key you want to check
  85. * @return boolean true is session variable is set, false if not
  86. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::check
  87. */
  88. public function check($name) {
  89. return CakeSession::check($name);
  90. }
  91. /**
  92. * Used to determine the last error in a session.
  93. *
  94. * In your controller: $this->Session->error();
  95. *
  96. * @return string Last session error
  97. */
  98. public function error() {
  99. return CakeSession::error();
  100. }
  101. /**
  102. * Used to set a session variable that can be used to output messages in the view.
  103. *
  104. * In your controller: $this->Session->setFlash('This has been saved');
  105. *
  106. * Additional params below can be passed to customize the output, or the Message.[key].
  107. * You can also set additional parameters when rendering flash messages. See SessionHelper::flash()
  108. * for more information on how to do that.
  109. *
  110. * @param string $message Message to be flashed
  111. * @param string $element Element to wrap flash message in.
  112. * @param array $params Parameters to be sent to layout as view variables
  113. * @param string $key Message key, default is 'flash'
  114. * @return void
  115. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#creating-notification-messages
  116. */
  117. public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
  118. CakeSession::write('Message.' . $key, compact('message', 'element', 'params'));
  119. }
  120. /**
  121. * Used to renew a session id
  122. *
  123. * In your controller: $this->Session->renew();
  124. *
  125. * @return void
  126. */
  127. public function renew() {
  128. return CakeSession::renew();
  129. }
  130. /**
  131. * Used to check for a valid session.
  132. *
  133. * In your controller: $this->Session->valid();
  134. *
  135. * @return boolean true is session is valid, false is session is invalid
  136. */
  137. public function valid() {
  138. return CakeSession::valid();
  139. }
  140. /**
  141. * Used to destroy sessions
  142. *
  143. * In your controller: $this->Session->destroy();
  144. *
  145. * @return void
  146. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::destroy
  147. */
  148. public function destroy() {
  149. return CakeSession::destroy();
  150. }
  151. /**
  152. * Returns Session id
  153. *
  154. * If $id is passed in a beforeFilter, the Session will be started
  155. * with the specified id
  156. *
  157. * @param string $id
  158. * @return string
  159. */
  160. public function id($id = null) {
  161. return CakeSession::id($id);
  162. }
  163. /**
  164. * Returns a bool, whether or not the session has been started.
  165. *
  166. * @return boolean
  167. */
  168. public function started() {
  169. return CakeSession::started();
  170. }
  171. }