Skip to content

Instantly share code, notes, and snippets.

@kazeburo
Created September 17, 2014 13:50
Show Gist options
  • Save kazeburo/95d51f44a6ae1c9cc20c to your computer and use it in GitHub Desktop.
Save kazeburo/95d51f44a6ae1c9cc20c to your computer and use it in GitHub Desktop.
use Kossy;
use Plack::Session;
sub Plack::Session::new {
my ($class, $env) = @_;
my $self = bless {
session => $env->{'psgix.session'},
options => $env->{'psgix.session.options'},
}, $class;
for my $flash_key ( grep { m/^__flash__/ } keys %{$self->session} ) {
delete $self->options->{no_store};
delete $self->session->{$flash_key};
my $key = substr($flash_key,9);
$self->{flash_session}->{$key} = delete $self->session->{$key};
}
}
sub Plack::Session::flash {
my $self = shift;
my $key = shift;
delete $self->options->{no_store};
if ( @_ ) {
$self->session->{'__flash__'.$key} = 1;
$self->session->{$key} = $_[0];
return $self->{flash_session}->{$key} = $_[0];
}
delete $self->session->{'__flash__'.$key};
delete $self->session->{$key};
delete $self->{flash_session}->{$key}
}
filter 'session' => sub {
my ($app) = @_;
sub {
my ($self, $c) = @_;
$c->stash->{session} = Plack::Session->new($c->req->env);
$app->($self, $c);
};
};
=pod
perl内では
$c->stash->{session}->(get|set|remove|flash)
テンプレートでは
$c.stash.session.(get|set|remove|flash)
my $val = $session->flash(key) ってやると$valが取得できて、sessionから消える
$session->flash(key,val) ってやると、次のリクエストもしくはflash(key)ってやると消えるデータがつっこめる
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment