Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ap/catalyst-plugin-flash
put values on the stash of the next request
https://github.com/ap/catalyst-plugin-flash
perl perl-catalyst
Last synced: about 2 months ago
JSON representation
put values on the stash of the next request
- Host: GitHub
- URL: https://github.com/ap/catalyst-plugin-flash
- Owner: ap
- Created: 2024-04-09T05:01:09.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2024-07-07T08:13:02.000Z (6 months ago)
- Last Synced: 2024-10-13T11:23:06.691Z (3 months ago)
- Topics: perl, perl-catalyst
- Language: Perl
- Homepage: https://metacpan.org/release/Catalyst-Plugin-Flash
- Size: 5.86 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.pod
- Changelog: Changes
Awesome Lists containing this project
README
use 5.008001; use strict; use warnings;
package Catalyst::Plugin::Flash;
our $VERSION = '0.002';
use URI ();
sub flash {
my ( $c, $uri, %var ) = ( shift, @_ );
( my $location = URI->new_abs( $uri, $c->request->uri )->as_string ) =~ s!#.*!!s;
@{ $c->{(__PACKAGE__)}{ $location } }{ keys %var } = values %var;
$uri;
}sub clear_flash { %{ $_[0]{(__PACKAGE__)} } = () }
########################################################################
sub flash_to_cookie { shift; @_ }
sub flash_from_cookie { shift; @_ }########################################################################
sub prepare_flash {
my $c = shift;
( my $location = $c->request->uri->as_string ) =~ s!#.*!!s;
my $cookie = $c->request->cookie( $location ) or return;
$c->stash( $c->flash_from_cookie( $cookie->value ) );
$c->response->cookies->{ $cookie->name } = { expires => '-1y', value => '' };
}sub finalize_flash {
my $c = shift;
my $flash = $c->{(__PACKAGE__)} or return;
my $uri = $c->response->status =~ /\A(?:30[12378]|201)\z/ && $c->response->location or return;
( my $location = URI->new_abs( $uri, $c->request->uri )->as_string ) =~ s!#.*!!s;
my @value = $c->flash_to_cookie( %{ $flash->{ $location } or return } );
$c->response->cookies->{ $location } = { expires => '+1m', value => \@value };
}########################################################################
# use Catalyst 5.80004 ();
use Moose::Role;
after prepare_path => sub { shift->prepare_flash };
before finalize_cookies => sub { shift->finalize_flash };
no Moose::Role;1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Catalyst::Plugin::Flash - put values on the stash of the next request
=head1 DESCRIPTION
This plugin will allow providing values that will automatically be placed
on the stash during a subsequent request.=cut