pictcode / lib / Cake / Controller / Component / SessionComponent.php @ a0ff9cef
履歴 | 表示 | アノテート | ダウンロード (6.135 KB)
| 1 |
<?php
|
|---|---|
| 2 |
/**
|
| 3 |
* SessionComponent. Provides access to Sessions from the Controller layer
|
| 4 |
*
|
| 5 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
| 6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
| 7 |
*
|
| 8 |
* Licensed under The MIT License
|
| 9 |
* For full copyright and license information, please see the LICENSE.txt
|
| 10 |
* Redistributions of files must retain the above copyright notice.
|
| 11 |
*
|
| 12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
| 13 |
* @link http://cakephp.org CakePHP(tm) Project
|
| 14 |
* @package Cake.Controller.Component
|
| 15 |
* @since CakePHP(tm) v 0.10.0.1232
|
| 16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
| 17 |
*/
|
| 18 |
|
| 19 |
App::uses('Component', 'Controller'); |
| 20 |
App::uses('CakeSession', 'Model/Datasource'); |
| 21 |
|
| 22 |
/**
|
| 23 |
* The CakePHP SessionComponent provides a way to persist client data between
|
| 24 |
* page requests. It acts as a wrapper for the `$_SESSION` as well as providing
|
| 25 |
* convenience methods for several `$_SESSION` related functions.
|
| 26 |
*
|
| 27 |
* @package Cake.Controller.Component
|
| 28 |
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
|
| 29 |
* @link http://book.cakephp.org/2.0/en/development/sessions.html
|
| 30 |
*/
|
| 31 |
class SessionComponent extends Component { |
| 32 |
|
| 33 |
/**
|
| 34 |
* Get / Set the userAgent
|
| 35 |
*
|
| 36 |
* @param string $userAgent Set the userAgent
|
| 37 |
* @return string Current user agent.
|
| 38 |
*/
|
| 39 |
public function userAgent($userAgent = null) { |
| 40 |
return CakeSession::userAgent($userAgent); |
| 41 |
} |
| 42 |
|
| 43 |
/**
|
| 44 |
* Writes a value to a session key.
|
| 45 |
*
|
| 46 |
* In your controller: $this->Session->write('Controller.sessKey', 'session value');
|
| 47 |
*
|
| 48 |
* @param string $name The name of the key your are setting in the session.
|
| 49 |
* This should be in a Controller.key format for better organizing
|
| 50 |
* @param string $value The value you want to store in a session.
|
| 51 |
* @return bool Success
|
| 52 |
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::write
|
| 53 |
*/
|
| 54 |
public function write($name, $value = null) { |
| 55 |
return CakeSession::write($name, $value); |
| 56 |
} |
| 57 |
|
| 58 |
/**
|
| 59 |
* Reads a session value for a key or returns values for all keys.
|
| 60 |
*
|
| 61 |
* In your controller: $this->Session->read('Controller.sessKey');
|
| 62 |
* Calling the method without a param will return all session vars
|
| 63 |
*
|
| 64 |
* @param string $name the name of the session key you want to read
|
| 65 |
* @return mixed value from the session vars
|
| 66 |
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::read
|
| 67 |
*/
|
| 68 |
public function read($name = null) { |
| 69 |
return CakeSession::read($name); |
| 70 |
} |
| 71 |
|
| 72 |
/**
|
| 73 |
* Deletes a session value for a key.
|
| 74 |
*
|
| 75 |
* In your controller: $this->Session->delete('Controller.sessKey');
|
| 76 |
*
|
| 77 |
* @param string $name the name of the session key you want to delete
|
| 78 |
* @return bool true is session variable is set and can be deleted, false is variable was not set.
|
| 79 |
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::delete
|
| 80 |
*/
|
| 81 |
public function delete($name) { |
| 82 |
return CakeSession::delete($name); |
| 83 |
} |
| 84 |
|
| 85 |
/**
|
| 86 |
* Reads and deletes a session value for a key.
|
| 87 |
*
|
| 88 |
* In your controller: `$this->Session->consume('Controller.sessKey');`
|
| 89 |
*
|
| 90 |
* @param string $name the name of the session key you want to read
|
| 91 |
* @return mixed values from the session vars
|
| 92 |
*/
|
| 93 |
public function consume($name) { |
| 94 |
return CakeSession::consume($name); |
| 95 |
} |
| 96 |
|
| 97 |
/**
|
| 98 |
* Checks if a session variable is set.
|
| 99 |
*
|
| 100 |
* In your controller: $this->Session->check('Controller.sessKey');
|
| 101 |
*
|
| 102 |
* @param string $name the name of the session key you want to check
|
| 103 |
* @return bool true is session variable is set, false if not
|
| 104 |
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::check
|
| 105 |
*/
|
| 106 |
public function check($name) { |
| 107 |
return CakeSession::check($name); |
| 108 |
} |
| 109 |
|
| 110 |
/**
|
| 111 |
* Used to determine the last error in a session.
|
| 112 |
*
|
| 113 |
* In your controller: $this->Session->error();
|
| 114 |
*
|
| 115 |
* @return string Last session error
|
| 116 |
*/
|
| 117 |
public function error() { |
| 118 |
return CakeSession::error(); |
| 119 |
} |
| 120 |
|
| 121 |
/**
|
| 122 |
* Used to set a session variable that can be used to output messages in the view.
|
| 123 |
*
|
| 124 |
* In your controller: $this->Session->setFlash('This has been saved');
|
| 125 |
*
|
| 126 |
* Additional params below can be passed to customize the output, or the Message.[key].
|
| 127 |
* You can also set additional parameters when rendering flash messages. See SessionHelper::flash()
|
| 128 |
* for more information on how to do that.
|
| 129 |
*
|
| 130 |
* @param string $message Message to be flashed
|
| 131 |
* @param string $element Element to wrap flash message in.
|
| 132 |
* @param array $params Parameters to be sent to layout as view variables
|
| 133 |
* @param string $key Message key, default is 'flash'
|
| 134 |
* @return void
|
| 135 |
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#creating-notification-messages
|
| 136 |
* @deprecated 3.0.0 Since 2.7, use the FlashComponent instead.
|
| 137 |
*/
|
| 138 |
public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') { |
| 139 |
CakeSession::write('Message.' . $key, compact('message', 'element', 'params')); |
| 140 |
} |
| 141 |
|
| 142 |
/**
|
| 143 |
* Used to renew a session id
|
| 144 |
*
|
| 145 |
* In your controller: $this->Session->renew();
|
| 146 |
*
|
| 147 |
* @return void
|
| 148 |
*/
|
| 149 |
public function renew() { |
| 150 |
return CakeSession::renew(); |
| 151 |
} |
| 152 |
|
| 153 |
/**
|
| 154 |
* Used to check for a valid session.
|
| 155 |
*
|
| 156 |
* In your controller: $this->Session->valid();
|
| 157 |
*
|
| 158 |
* @return bool true is session is valid, false is session is invalid
|
| 159 |
*/
|
| 160 |
public function valid() { |
| 161 |
return CakeSession::valid(); |
| 162 |
} |
| 163 |
|
| 164 |
/**
|
| 165 |
* Used to destroy sessions
|
| 166 |
*
|
| 167 |
* In your controller: $this->Session->destroy();
|
| 168 |
*
|
| 169 |
* @return void
|
| 170 |
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::destroy
|
| 171 |
*/
|
| 172 |
public function destroy() { |
| 173 |
return CakeSession::destroy(); |
| 174 |
} |
| 175 |
|
| 176 |
/**
|
| 177 |
* Get/Set the session id.
|
| 178 |
*
|
| 179 |
* When fetching the session id, the session will be started
|
| 180 |
* if it has not already been started. When setting the session id,
|
| 181 |
* the session will not be started.
|
| 182 |
*
|
| 183 |
* @param string $id Id to use (optional)
|
| 184 |
* @return string The current session id.
|
| 185 |
*/
|
| 186 |
public function id($id = null) { |
| 187 |
if (empty($id)) { |
| 188 |
CakeSession::start();
|
| 189 |
} |
| 190 |
return CakeSession::id($id); |
| 191 |
} |
| 192 |
|
| 193 |
/**
|
| 194 |
* Returns a bool, whether or not the session has been started.
|
| 195 |
*
|
| 196 |
* @return bool
|
| 197 |
*/
|
| 198 |
public function started() { |
| 199 |
return CakeSession::started(); |
| 200 |
} |
| 201 |
|
| 202 |
} |