flash message

66 views
Skip to first unread message

Miguel Prz

unread,
Jun 29, 2013, 10:18:06 AM6/29/13
to perl...@googlegroups.com
Hello, I'm looking for a flash message functionality, something like this (for Dancer):

I don't know if there is a simple way to do this in Kelp.

Thanks!

Stefan Geneshky

unread,
Jun 29, 2013, 11:22:19 AM6/29/13
to perl...@googlegroups.com
FlashMessage is a Dancer plugin. Kelp doesn't have an equivalent module yet, but should be relatively easy to write one. Feel free to do it.
All you need is session management, so you'll have to include the Session middleware.
Let me know if you need more detailed help.

Stefan

miguel prz

unread,
Jun 29, 2013, 12:25:23 PM6/29/13
to perl...@googlegroups.com
yes, I'm curious how to use the Plack Session on a Kelp route, according this link:

the Plack::Middleware::Session needs the $env, ... how can I get it ?

Thanks


2013/6/29 Stefan Geneshky <stefa...@gmail.com>

miguel prz

unread,
Jun 29, 2013, 12:45:16 PM6/29/13
to perl...@googlegroups.com
auto-answer:
sub my_route {
   my $self = shift;
   my $session = Plack::Session->new($self->req);
   #...
}

Also I have seen that the route methods belongs to the app namespace (I package them into a custom Controller namespace and that confused me). I have declared a sub session { ... } on the main app namespace and I call it from the routes to get the plack session object. 



2013/6/29 miguel prz <nice...@gmail.com>

Stefan Geneshky

unread,
Jun 29, 2013, 12:51:52 PM6/29/13
to perl...@googlegroups.com

Miguel,

This is a wrong approach and will not work well. You need to add the session as middleware. The easiest way to do this is via the config file. Look up Kelp's documentation under "Adding middleware".
You can also see one of the other questions here in the mailing list for an example of how to initialize sessions.

Stefan

Miguel Prz

unread,
Jun 29, 2013, 1:26:35 PM6/29/13
to perl...@googlegroups.com
yes, I added the session middleware, and initialized it 

my question was about how to use the plack session from a route
why my approach won't work well ?

thanks!

miguel prz

unread,
Jun 29, 2013, 2:10:20 PM6/29/13
to perl...@googlegroups.com
I have read the docs, and finally I found the answer...
$self->req->env->{'psgix.session'}
gives me access to the session hash. Previously, I initialized the Plack::Middleware::Session with the config file.

And finally, I added a custom module to handle the flash message, following the ad you gave me. I'm thinking about to package it and distribute on CPAN.

thanks!


2013/6/29 Miguel Prz <nice...@gmail.com>

Stefan Geneshky

unread,
Jun 29, 2013, 2:18:31 PM6/29/13
to perl...@googlegroups.com
Once you have the session middleware, you access the session via the Plack env.

sub some_route {
    my $self = shift;
    my $session = $self->req->env->{psgix.session};
    # Do something with $session now
}

Now that I see this it looks a little long, so I'll be adding a $self->session shortcut in the next Kelp release.

To make things a little clearer for you, here is what's happening: 

$self->req contains the Kelp::Request object for this route. Kelp::Request has some helpful methods, but it also inherits from Plack::Request.
If you look at Plack::Request's docs, you'll see that "env" gives you the Plack environment. Now if you look at the Plack::Middleware::Session docs, you'll see that you get the session via the "psgix.session" key.

I hope that helps.

Stefan Geneshky

unread,
Jun 29, 2013, 2:19:27 PM6/29/13
to perl...@googlegroups.com
Ok, great. I didn't see your last email, but I'm happy you figured it out on your own.

Miguel Prz

unread,
Jun 29, 2013, 2:46:49 PM6/29/13
to perl...@googlegroups.com
Stefan,
your idea: $self->session
is great, ... will it return a plain hash or a custom Kelp::Session (that inhereits from Plack::Session) ?
thanks

Stefan Geneshky

unread,
Jun 30, 2013, 9:42:06 PM6/30/13
to perl...@googlegroups.com
I'd like to keep things simple. It will return a plain hash, but it will also provide a flexible interface to it.
I already committed the code to GitHub. Here is a link to the session POD:

This, and a few other new features and fixes will find their way to CPAN sometime next week.

Stefan

Brad

unread,
Jul 23, 2014, 4:49:18 AM7/23/14
to perl...@googlegroups.com

This is an old thread, but figured I'd post this in case it helps someone else. I use a custom flash plugin that works pretty well, and didn't take much to write! I love Plack.

use Moo;
extends 'Kelp::Module';

sub build {
  my ($self, %args) = @_;
  $self->register(
    flash_set => sub {
      my ($self, $key, $val) = @_;
      if ($val) {
        $self->session->{"_flash_${key}"} = $val;
      }
    },

    flash_get => sub {
      my ($self, $key) = @_;
      if (my $val = $self->session->{"_flash_${key}"}) {
        delete $self->session->{"_flash_${key}"};
        return $val;
      }
    },
  );
}

 

Brad

unread,
Jul 23, 2014, 4:51:04 AM7/23/14
to perl...@googlegroups.com
Obviously it's just $self->flash_set('some_option', 'some value'), then the next to you retrieve it with $self->flash_get('some_option'), it returns the value then removes the temporary session.
Reply all
Reply to author
Forward
0 new messages