Я хотел бы узнать лучший способ отображения флеш-сообщений в Kohana v3?
Некоторые учебные пособия или примеры были бы полезными.
Вы имеете в виду, как переменные сеанса сеанса Kohana 2.x?
Последняя get_once()
Kohana поддерживает get_once()
которая очень похожа на старые переменные сеанса get_once()
.
$session = Session::instance(); $session->set('test', 'Hello, World!'); // The session variable is returned and removed. $test = $session->get_once('test');
Я думаю, что get_once – отличная функция, но что, если вы хотите, чтобы данные фактически были отделены от обычных данных, вот базовый класс, который перегружает «Сессию», чтобы вы могли использовать вызовы flashdata стиля «codeigniter» с любым хранилищем данных ,
<?php defined('SYSPATH') or die('No direct script access.'); abstract class Session extends Kohana_Session { /** * This calls the parent Kohana_Session constructor and processes * new flashdata to flashdata, and flashdata to old flashdata * * @param array configuration * @param string session id * @return void * @uses Kohana_Session::__construct */ public function __construct(array $config = NULL, $id = NULL) { parent::__construct($config,$id); if(array_key_exists('___of',$this->_data)){ //Remove old Flash data unset($this->_data['___of']); } if(array_key_exists('___flash',$this->_data)){ //Move current last requests flash data to old flash data $this->_data['___of'] = $this->_data['___flash']; unset($this->_data['___flash']); } if(array_key_exists('___nf',$this->_data)){ //Move Last Requests added data to the flash data $this->_data['___flash'] = $this->_data['___nf']; unset($this->_data['___nf']); } } /** * keeps a variable set in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param ... * @return $this */ public function keep_flashdata($k) { $args = func_get_args(); if(array_key_exists('___of',$this->_data)){ foreach($args as $key){ if(array_key_exists($key,$this->_data['___of'])){ //So we were going to trash it... $this->set_flashdata($k,$this->_data['___of'][$key],true); } } } $this->_data['___nf'][$key] = $value; return $this; } /** * Set a variable in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param mixed value * @return $this */ public function set_flashdata($key, $value, $current=false) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] = $value; if($current){ if(!array_key_exists('___flash',$this->_data)){ $this->_data['___flash'] = array(); } $this->_data['flash'][$key] = $value; } return $this; } /** * Set a variable by reference in the sessions flashdata array. * * $session->bind_flashdata('foo', $foo); * * @param string variable name * @param mixed referenced value * @return $this */ public function bind_flashdata($key, & $value) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] =& $value; return $this; } /** * Removes a variable in the session array. * * $session->delete_flashdata('foo'); * * @param string variable name * @param ... * @return $this */ public function delete_flashdata($key) { $args = func_get_args(); if(array_key_exists('___nf',$this->_data)){ foreach ($args as $key) { if(array_key_exists($key,$this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } } } return $this; } /** * Get a variable from the sessions flashdata array. * * $foo = $session->get_flashdata('foo'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata($key, $default = NULL) { if(array_key_exists('___flash',$this->_data) && array_key_exists($key,$this->_data['___flash'])){ return $this->_data['___flash'][$key]; } else if(array_key_exists('___nf',$this->_data) && array_key_exists($key,$this->_data['___nf'])){ return $this->_data['___nf'][$key]; } return $default; } /** * Get and delete a variable from the session array. * * $bar = $session->get_once('bar'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata_once($key, $default = NULL) { $value = $this->get_flashdata($key, $default); if(array_key_exists($key, $this->_data['___flash'])){ unset($this->_data['___flash'][$key]); } if(array_key_exists($key, $this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } return $value; } } ?>
с<?php defined('SYSPATH') or die('No direct script access.'); abstract class Session extends Kohana_Session { /** * This calls the parent Kohana_Session constructor and processes * new flashdata to flashdata, and flashdata to old flashdata * * @param array configuration * @param string session id * @return void * @uses Kohana_Session::__construct */ public function __construct(array $config = NULL, $id = NULL) { parent::__construct($config,$id); if(array_key_exists('___of',$this->_data)){ //Remove old Flash data unset($this->_data['___of']); } if(array_key_exists('___flash',$this->_data)){ //Move current last requests flash data to old flash data $this->_data['___of'] = $this->_data['___flash']; unset($this->_data['___flash']); } if(array_key_exists('___nf',$this->_data)){ //Move Last Requests added data to the flash data $this->_data['___flash'] = $this->_data['___nf']; unset($this->_data['___nf']); } } /** * keeps a variable set in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param ... * @return $this */ public function keep_flashdata($k) { $args = func_get_args(); if(array_key_exists('___of',$this->_data)){ foreach($args as $key){ if(array_key_exists($key,$this->_data['___of'])){ //So we were going to trash it... $this->set_flashdata($k,$this->_data['___of'][$key],true); } } } $this->_data['___nf'][$key] = $value; return $this; } /** * Set a variable in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param mixed value * @return $this */ public function set_flashdata($key, $value, $current=false) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] = $value; if($current){ if(!array_key_exists('___flash',$this->_data)){ $this->_data['___flash'] = array(); } $this->_data['flash'][$key] = $value; } return $this; } /** * Set a variable by reference in the sessions flashdata array. * * $session->bind_flashdata('foo', $foo); * * @param string variable name * @param mixed referenced value * @return $this */ public function bind_flashdata($key, & $value) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] =& $value; return $this; } /** * Removes a variable in the session array. * * $session->delete_flashdata('foo'); * * @param string variable name * @param ... * @return $this */ public function delete_flashdata($key) { $args = func_get_args(); if(array_key_exists('___nf',$this->_data)){ foreach ($args as $key) { if(array_key_exists($key,$this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } } } return $this; } /** * Get a variable from the sessions flashdata array. * * $foo = $session->get_flashdata('foo'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata($key, $default = NULL) { if(array_key_exists('___flash',$this->_data) && array_key_exists($key,$this->_data['___flash'])){ return $this->_data['___flash'][$key]; } else if(array_key_exists('___nf',$this->_data) && array_key_exists($key,$this->_data['___nf'])){ return $this->_data['___nf'][$key]; } return $default; } /** * Get and delete a variable from the session array. * * $bar = $session->get_once('bar'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata_once($key, $default = NULL) { $value = $this->get_flashdata($key, $default); if(array_key_exists($key, $this->_data['___flash'])){ unset($this->_data['___flash'][$key]); } if(array_key_exists($key, $this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } return $value; } } ?>
с<?php defined('SYSPATH') or die('No direct script access.'); abstract class Session extends Kohana_Session { /** * This calls the parent Kohana_Session constructor and processes * new flashdata to flashdata, and flashdata to old flashdata * * @param array configuration * @param string session id * @return void * @uses Kohana_Session::__construct */ public function __construct(array $config = NULL, $id = NULL) { parent::__construct($config,$id); if(array_key_exists('___of',$this->_data)){ //Remove old Flash data unset($this->_data['___of']); } if(array_key_exists('___flash',$this->_data)){ //Move current last requests flash data to old flash data $this->_data['___of'] = $this->_data['___flash']; unset($this->_data['___flash']); } if(array_key_exists('___nf',$this->_data)){ //Move Last Requests added data to the flash data $this->_data['___flash'] = $this->_data['___nf']; unset($this->_data['___nf']); } } /** * keeps a variable set in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param ... * @return $this */ public function keep_flashdata($k) { $args = func_get_args(); if(array_key_exists('___of',$this->_data)){ foreach($args as $key){ if(array_key_exists($key,$this->_data['___of'])){ //So we were going to trash it... $this->set_flashdata($k,$this->_data['___of'][$key],true); } } } $this->_data['___nf'][$key] = $value; return $this; } /** * Set a variable in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param mixed value * @return $this */ public function set_flashdata($key, $value, $current=false) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] = $value; if($current){ if(!array_key_exists('___flash',$this->_data)){ $this->_data['___flash'] = array(); } $this->_data['flash'][$key] = $value; } return $this; } /** * Set a variable by reference in the sessions flashdata array. * * $session->bind_flashdata('foo', $foo); * * @param string variable name * @param mixed referenced value * @return $this */ public function bind_flashdata($key, & $value) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] =& $value; return $this; } /** * Removes a variable in the session array. * * $session->delete_flashdata('foo'); * * @param string variable name * @param ... * @return $this */ public function delete_flashdata($key) { $args = func_get_args(); if(array_key_exists('___nf',$this->_data)){ foreach ($args as $key) { if(array_key_exists($key,$this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } } } return $this; } /** * Get a variable from the sessions flashdata array. * * $foo = $session->get_flashdata('foo'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata($key, $default = NULL) { if(array_key_exists('___flash',$this->_data) && array_key_exists($key,$this->_data['___flash'])){ return $this->_data['___flash'][$key]; } else if(array_key_exists('___nf',$this->_data) && array_key_exists($key,$this->_data['___nf'])){ return $this->_data['___nf'][$key]; } return $default; } /** * Get and delete a variable from the session array. * * $bar = $session->get_once('bar'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata_once($key, $default = NULL) { $value = $this->get_flashdata($key, $default); if(array_key_exists($key, $this->_data['___flash'])){ unset($this->_data['___flash'][$key]); } if(array_key_exists($key, $this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } return $value; } } ?>
с<?php defined('SYSPATH') or die('No direct script access.'); abstract class Session extends Kohana_Session { /** * This calls the parent Kohana_Session constructor and processes * new flashdata to flashdata, and flashdata to old flashdata * * @param array configuration * @param string session id * @return void * @uses Kohana_Session::__construct */ public function __construct(array $config = NULL, $id = NULL) { parent::__construct($config,$id); if(array_key_exists('___of',$this->_data)){ //Remove old Flash data unset($this->_data['___of']); } if(array_key_exists('___flash',$this->_data)){ //Move current last requests flash data to old flash data $this->_data['___of'] = $this->_data['___flash']; unset($this->_data['___flash']); } if(array_key_exists('___nf',$this->_data)){ //Move Last Requests added data to the flash data $this->_data['___flash'] = $this->_data['___nf']; unset($this->_data['___nf']); } } /** * keeps a variable set in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param ... * @return $this */ public function keep_flashdata($k) { $args = func_get_args(); if(array_key_exists('___of',$this->_data)){ foreach($args as $key){ if(array_key_exists($key,$this->_data['___of'])){ //So we were going to trash it... $this->set_flashdata($k,$this->_data['___of'][$key],true); } } } $this->_data['___nf'][$key] = $value; return $this; } /** * Set a variable in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param mixed value * @return $this */ public function set_flashdata($key, $value, $current=false) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] = $value; if($current){ if(!array_key_exists('___flash',$this->_data)){ $this->_data['___flash'] = array(); } $this->_data['flash'][$key] = $value; } return $this; } /** * Set a variable by reference in the sessions flashdata array. * * $session->bind_flashdata('foo', $foo); * * @param string variable name * @param mixed referenced value * @return $this */ public function bind_flashdata($key, & $value) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] =& $value; return $this; } /** * Removes a variable in the session array. * * $session->delete_flashdata('foo'); * * @param string variable name * @param ... * @return $this */ public function delete_flashdata($key) { $args = func_get_args(); if(array_key_exists('___nf',$this->_data)){ foreach ($args as $key) { if(array_key_exists($key,$this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } } } return $this; } /** * Get a variable from the sessions flashdata array. * * $foo = $session->get_flashdata('foo'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata($key, $default = NULL) { if(array_key_exists('___flash',$this->_data) && array_key_exists($key,$this->_data['___flash'])){ return $this->_data['___flash'][$key]; } else if(array_key_exists('___nf',$this->_data) && array_key_exists($key,$this->_data['___nf'])){ return $this->_data['___nf'][$key]; } return $default; } /** * Get and delete a variable from the session array. * * $bar = $session->get_once('bar'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata_once($key, $default = NULL) { $value = $this->get_flashdata($key, $default); if(array_key_exists($key, $this->_data['___flash'])){ unset($this->_data['___flash'][$key]); } if(array_key_exists($key, $this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } return $value; } } ?>
с<?php defined('SYSPATH') or die('No direct script access.'); abstract class Session extends Kohana_Session { /** * This calls the parent Kohana_Session constructor and processes * new flashdata to flashdata, and flashdata to old flashdata * * @param array configuration * @param string session id * @return void * @uses Kohana_Session::__construct */ public function __construct(array $config = NULL, $id = NULL) { parent::__construct($config,$id); if(array_key_exists('___of',$this->_data)){ //Remove old Flash data unset($this->_data['___of']); } if(array_key_exists('___flash',$this->_data)){ //Move current last requests flash data to old flash data $this->_data['___of'] = $this->_data['___flash']; unset($this->_data['___flash']); } if(array_key_exists('___nf',$this->_data)){ //Move Last Requests added data to the flash data $this->_data['___flash'] = $this->_data['___nf']; unset($this->_data['___nf']); } } /** * keeps a variable set in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param ... * @return $this */ public function keep_flashdata($k) { $args = func_get_args(); if(array_key_exists('___of',$this->_data)){ foreach($args as $key){ if(array_key_exists($key,$this->_data['___of'])){ //So we were going to trash it... $this->set_flashdata($k,$this->_data['___of'][$key],true); } } } $this->_data['___nf'][$key] = $value; return $this; } /** * Set a variable in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param mixed value * @return $this */ public function set_flashdata($key, $value, $current=false) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] = $value; if($current){ if(!array_key_exists('___flash',$this->_data)){ $this->_data['___flash'] = array(); } $this->_data['flash'][$key] = $value; } return $this; } /** * Set a variable by reference in the sessions flashdata array. * * $session->bind_flashdata('foo', $foo); * * @param string variable name * @param mixed referenced value * @return $this */ public function bind_flashdata($key, & $value) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] =& $value; return $this; } /** * Removes a variable in the session array. * * $session->delete_flashdata('foo'); * * @param string variable name * @param ... * @return $this */ public function delete_flashdata($key) { $args = func_get_args(); if(array_key_exists('___nf',$this->_data)){ foreach ($args as $key) { if(array_key_exists($key,$this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } } } return $this; } /** * Get a variable from the sessions flashdata array. * * $foo = $session->get_flashdata('foo'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata($key, $default = NULL) { if(array_key_exists('___flash',$this->_data) && array_key_exists($key,$this->_data['___flash'])){ return $this->_data['___flash'][$key]; } else if(array_key_exists('___nf',$this->_data) && array_key_exists($key,$this->_data['___nf'])){ return $this->_data['___nf'][$key]; } return $default; } /** * Get and delete a variable from the session array. * * $bar = $session->get_once('bar'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata_once($key, $default = NULL) { $value = $this->get_flashdata($key, $default); if(array_key_exists($key, $this->_data['___flash'])){ unset($this->_data['___flash'][$key]); } if(array_key_exists($key, $this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } return $value; } } ?>
с<?php defined('SYSPATH') or die('No direct script access.'); abstract class Session extends Kohana_Session { /** * This calls the parent Kohana_Session constructor and processes * new flashdata to flashdata, and flashdata to old flashdata * * @param array configuration * @param string session id * @return void * @uses Kohana_Session::__construct */ public function __construct(array $config = NULL, $id = NULL) { parent::__construct($config,$id); if(array_key_exists('___of',$this->_data)){ //Remove old Flash data unset($this->_data['___of']); } if(array_key_exists('___flash',$this->_data)){ //Move current last requests flash data to old flash data $this->_data['___of'] = $this->_data['___flash']; unset($this->_data['___flash']); } if(array_key_exists('___nf',$this->_data)){ //Move Last Requests added data to the flash data $this->_data['___flash'] = $this->_data['___nf']; unset($this->_data['___nf']); } } /** * keeps a variable set in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param ... * @return $this */ public function keep_flashdata($k) { $args = func_get_args(); if(array_key_exists('___of',$this->_data)){ foreach($args as $key){ if(array_key_exists($key,$this->_data['___of'])){ //So we were going to trash it... $this->set_flashdata($k,$this->_data['___of'][$key],true); } } } $this->_data['___nf'][$key] = $value; return $this; } /** * Set a variable in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param mixed value * @return $this */ public function set_flashdata($key, $value, $current=false) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] = $value; if($current){ if(!array_key_exists('___flash',$this->_data)){ $this->_data['___flash'] = array(); } $this->_data['flash'][$key] = $value; } return $this; } /** * Set a variable by reference in the sessions flashdata array. * * $session->bind_flashdata('foo', $foo); * * @param string variable name * @param mixed referenced value * @return $this */ public function bind_flashdata($key, & $value) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] =& $value; return $this; } /** * Removes a variable in the session array. * * $session->delete_flashdata('foo'); * * @param string variable name * @param ... * @return $this */ public function delete_flashdata($key) { $args = func_get_args(); if(array_key_exists('___nf',$this->_data)){ foreach ($args as $key) { if(array_key_exists($key,$this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } } } return $this; } /** * Get a variable from the sessions flashdata array. * * $foo = $session->get_flashdata('foo'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata($key, $default = NULL) { if(array_key_exists('___flash',$this->_data) && array_key_exists($key,$this->_data['___flash'])){ return $this->_data['___flash'][$key]; } else if(array_key_exists('___nf',$this->_data) && array_key_exists($key,$this->_data['___nf'])){ return $this->_data['___nf'][$key]; } return $default; } /** * Get and delete a variable from the session array. * * $bar = $session->get_once('bar'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata_once($key, $default = NULL) { $value = $this->get_flashdata($key, $default); if(array_key_exists($key, $this->_data['___flash'])){ unset($this->_data['___flash'][$key]); } if(array_key_exists($key, $this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } return $value; } } ?>
с<?php defined('SYSPATH') or die('No direct script access.'); abstract class Session extends Kohana_Session { /** * This calls the parent Kohana_Session constructor and processes * new flashdata to flashdata, and flashdata to old flashdata * * @param array configuration * @param string session id * @return void * @uses Kohana_Session::__construct */ public function __construct(array $config = NULL, $id = NULL) { parent::__construct($config,$id); if(array_key_exists('___of',$this->_data)){ //Remove old Flash data unset($this->_data['___of']); } if(array_key_exists('___flash',$this->_data)){ //Move current last requests flash data to old flash data $this->_data['___of'] = $this->_data['___flash']; unset($this->_data['___flash']); } if(array_key_exists('___nf',$this->_data)){ //Move Last Requests added data to the flash data $this->_data['___flash'] = $this->_data['___nf']; unset($this->_data['___nf']); } } /** * keeps a variable set in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param ... * @return $this */ public function keep_flashdata($k) { $args = func_get_args(); if(array_key_exists('___of',$this->_data)){ foreach($args as $key){ if(array_key_exists($key,$this->_data['___of'])){ //So we were going to trash it... $this->set_flashdata($k,$this->_data['___of'][$key],true); } } } $this->_data['___nf'][$key] = $value; return $this; } /** * Set a variable in the sessions flashdata array. * * $session->set_flashdata('foo', 'bar'); * * @param string variable name * @param mixed value * @return $this */ public function set_flashdata($key, $value, $current=false) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] = $value; if($current){ if(!array_key_exists('___flash',$this->_data)){ $this->_data['___flash'] = array(); } $this->_data['flash'][$key] = $value; } return $this; } /** * Set a variable by reference in the sessions flashdata array. * * $session->bind_flashdata('foo', $foo); * * @param string variable name * @param mixed referenced value * @return $this */ public function bind_flashdata($key, & $value) { if(!array_key_exists('___nf',$this->_data)){ $this->_data['___nf'] = array(); } $this->_data['___nf'][$key] =& $value; return $this; } /** * Removes a variable in the session array. * * $session->delete_flashdata('foo'); * * @param string variable name * @param ... * @return $this */ public function delete_flashdata($key) { $args = func_get_args(); if(array_key_exists('___nf',$this->_data)){ foreach ($args as $key) { if(array_key_exists($key,$this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } } } return $this; } /** * Get a variable from the sessions flashdata array. * * $foo = $session->get_flashdata('foo'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata($key, $default = NULL) { if(array_key_exists('___flash',$this->_data) && array_key_exists($key,$this->_data['___flash'])){ return $this->_data['___flash'][$key]; } else if(array_key_exists('___nf',$this->_data) && array_key_exists($key,$this->_data['___nf'])){ return $this->_data['___nf'][$key]; } return $default; } /** * Get and delete a variable from the session array. * * $bar = $session->get_once('bar'); * * @param string variable name * @param mixed default value to return * @return mixed */ public function get_flashdata_once($key, $default = NULL) { $value = $this->get_flashdata($key, $default); if(array_key_exists($key, $this->_data['___flash'])){ unset($this->_data['___flash'][$key]); } if(array_key_exists($key, $this->_data['___nf'])){ unset($this->_data['___nf'][$key]); } return $value; } } ?>
Я понимаю, что был ответ на этот вопрос, и, как я уже говорил, метод get_once велик, и все, но я люблю сборку мусора намного больше.
Если у вас есть какие-либо улучшения в этом коде, дайте мне знать, это было здорово для меня до сих пор.
Посмотрите на этот модуль, это может быть то, что вы ищете https://github.com/daveWid/message
На этот раз я написал действительно простой класс. Проверьте это ниже. Примеры использования ниже
class Notice { private static $session; private static $initialized = false; // current notices private static $notices = array(); function __construct() { } static function init() { self::$session = Session::instance(); self::$notices['current'] = json_decode(self::$session->get_once('flash')); if(!is_array(self::$notices['current'])) self::$notices['current'] = array(); self::$initialized = true; } static function add($notice, $key=null) { if(!self::$initialized) self::init(); if(!is_null($key)) { self::$notices['new'][$key] = $notice; } else { self::$notices['new'][] = $notice; } self::$session->set('flash', json_encode(self::$notices['new'])); return true; } static function get($item = null) { if(!self::$initialized) self::init(); if($item == null) { return self::$notices['current']; } if(!array_key_exists($item, self::$notices['current'])) return null; return self::$notices['current'][$item]; } }
Примеры (если этот класс сохранен как APPPATH . 'classes/notice.php'
):
Notice::add('Something great has happened!'); Notice::add('Exciting! I\'ve got something to tell you!', 'message'); echo Notice::get('message'); // "Exciting! I've got ..." foreach(Notice::get() as $message) { echo $i++ . $message .'<br />'; }
EDIT: смешно … почему-то этот вопрос появился где-то, не заметил, что это действительно старый … извините за это!
Я использую https://github.com/synapsestudios/kohana-notices в своем проекте, и я очень доволен этим.