LoginSignup
19
22

More than 5 years have passed since last update.

【CakePHP】Sessionコンポーネント

Last updated at Posted at 2014-10-10

セッション管理には、SessionComponentを使用する。

定義

コンポーネントなので定義が必要だけど、AppControllerへデフォルトで設定されている。

public $components = array(
  'Session',
);

基本編

セッションはキーと値で管理される。
値には配列やオブジェクトを設定することも可能。

//セッションに値を設定する
$this->Session->write('key','value');

//値を取得する
$key = $this->Session->read('key');

//値を削除する
$this->Session->delete('key');

まとめて設定する

まとめて設定することもできる。

//まとめてセッションに値を設定する
$this->Session->write(array(
  'key1'=>'value1',
  'key2'=>'value2',
));

グループ化する

「.」を使ってキーをグループ化することが可能。

//グループでセッションに値を設定する
$this->Session->write('Group.key1','value1');
$this->Session->write('Group.key2','value2');

//グループごと取得する
$group = $this->Session->write('Group','value');
var_dump($group);
/*
*array(
*  'key1'=>'value1',
*  'key2'=>'value2',
*)
*/

//もちろん個別でも可能
$group = $this->Session->write('Group.key1','value');
19
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
22