ivva
ivva

Reputation: 2949

How to display flash message in Kohana 3

I have to show message after insert some data in database. I'm using Kohana. Is there a way to do that with flash messages? It's better than header refresh.

Upvotes: 0

Views: 482

Answers (2)

giorgio
giorgio

Reputation: 10202

Well sort of. You could use the Session::get_once() function. But this only let you retrieve a variable once, and you cannot use it again in the same request. While you want a flash message to persist a full request cycle. To manage that you'll need a wrapper class, something like this.


class Flash {
    private $session;
    private $messages = array();

    private static $_instance; // Singleton object

    public static function instance() {
        if ( ! isset( self::$_instance ) ) {
            self::$_instance = new Flash();
        }

        return self::$_instance;
    }

    private function __construct() {
        $this->session = Session::instance();
        $this->messages['current'] = $this->session->get_once('flash');
        if( ! is_array($this->messages['current'] ) ) {
            $this->messages['current'] = array();
        }
    }

    public function add( $key, $message === null ) {
        if ( is_null( $message ) ) {
            $message = $key;
            $key = null;
        }
        $this->messages['new'][$key] = $message;
        $this->session->set('flash', $this->messages['new'] );
        return true;
    }

    public function get( $item = null ) {
        if( $item === null ) {
            return $this->messages['current'];
        }
        if( ! array_key_exists($item, $this->messages['current']) ) {
            return null;
        }
        return $this->messages['current'][$item];
    }

}

Usage:

$flash = Flash::instance();

$flash->add('A random message');
$flash->add('some_key', 'Some message');

$flash->get(); // array( 0 => 'A random message', 'some_key' => 'Some message')
$flash->get('some_key'); // 'A Random message'

What it does basically is on initialization it retrieves the current message from the session, using the get_once() function. The variable is nou out of the Session object, so it will only last this request. Everytime you add a variable, it will immediately persisted to the Session object.

There is just one problem; if you are using ajax calls, the messages will only be available on the initial php request, not on subsequent ajax calls. And there is also no restriction whatsoever on what kind of variable you are storing (but it must be serializable). You'll have to build in some checks for that too.

warning: the class is not tested, so it would surprise me if you do not get a syntax error ;)

And to go a step further: you would need an extra refresh anyway. The request flow should be like this imo:

Request 1: User is presented form Request 2: User posts the form, which is processed. Data is inserted in database. When done, user is redirected Request 3: A confirmation page is shown (can be "thank you", or the detail page, whatever).

You would set the flash message in request 2, and show it in 3. I would not directly show the thank you page on request 2, because when the user refreshes, the form will be posted again.

Upvotes: 2

Related Questions