这是显示“闪现消息”的最佳方式在科哈纳 v3 中?

发布于 2024-10-12 04:35:41 字数 58 浏览 9 评论 0原文

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

一些教程或示例会很有帮助。

I would like to know the best way to display flash messages in Kohana v3?

Some tutorials or examples would be helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挽清梦 2024-10-19 04:35:41

你的意思是像 Kohana 2.x 的 flash 会话变量吗?

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

$session = Session::instance();

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

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

Do you mean like Kohana 2.x's flash session variables?

The latest Kohana supports get_once() which is pretty similar to the old flash session variables.

$session = Session::instance();

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

// The session variable is returned and removed.
$test = $session->get_once('test');
十六岁半 2024-10-19 04:35:41

我认为 get_once 是一个很棒的函数,但是如果您想让数据实际上与常规数据分开,这里有一个重载“Session”的基本类,以便您可以对任何数据存储使用“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 方法很棒,但我更喜欢自动垃圾收集。

如果您对此代码有任何改进,请告诉我,到目前为止它对我来说非常好。

I think the get_once is a great function, but what if you want to keep the data actually separate from the regular data, here's a basic class that overloads "Session" so that you can use "codeigniter" style flashdata calls with any data-store.

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

I realize there was an answer to this, and like i stated before, the get_once method is great and all, but i enjoy auto garbage collection much more.

If you have any improvements on this code, let me know, its been great to me so far.

陈年往事 2024-10-19 04:35:41

看看这个模块,它可能就是您正在寻找的 https://github.com/daveWid/message< /a>

Have a look at this module, it might be what you are looking for https://github.com/daveWid/message

喜你已久 2024-10-19 04:35:41

我曾经为此编写过一个非常简单的类。看看下面。下面的用法示例

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

编辑:有趣...出于某种原因,这个问题在某处弹出,没有注意到它是一个非常旧的...抱歉!

I've written a really simple class for this once. Check it out below. Usage examples below

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];
    }
}

Examples (provided this class is saved as 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: funny... for some reason this question popped up somewhere, didn't notice it was a really old one... sorry for that!

っ〆星空下的拥抱 2024-10-19 04:35:41

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

I am using https://github.com/synapsestudios/kohana-notices in my project and I am very happy with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文