我想知道的最好的方式显示 闪消息 在Kohana v3?

一些教程或实例会有帮助。

有帮助吗?

解决方案

您的意思是像Kohana 2.x的闪存会话变量吗?

最新的Kohana支持 get_once() 这与旧的Flash会话变量非常相似。

$session = Session::instance();

$session->set('test', 'Hello, World!');

// The session variable is returned and removed.
$test = $session->get_once('test');

其他提示

我认为get_once是一个很棒的功能,但是如果您想将数据实际与常规数据分开,这是一个基本类,可以使用“ codeigniter”样式的flashdata调用与任何数据存储。

<?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 />';
}

编辑:有趣的...由于某些原因,这个问题出现的地方,没有注意到它是一个真正的古老的一个...对不起!

我在用 https://github.com/synapsestudios/kohana-notices 在我的项目中,我对此感到非常满意。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top