/
Session.pm
87 lines (57 loc) · 1.77 KB
/
Session.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package Mojolicious::Plugin::Session;
use strict;
use warnings;
use base 'Mojolicious::Plugin';
use MojoX::Session;
sub register {
my ($self, $app, $args) = @_;
$args ||= {};
my $stash_key = delete $args->{stash_key} || 'mojox-session';
my $init = delete $args->{init};
$app->hook(
before_dispatch => sub {
my $self = shift;
my $session = MojoX::Session->new(%$args);
$session->tx($self->tx);
$init->($self, $session) if $init;
$self->stash($stash_key => $session);
}
);
$app->hook(
after_dispatch => sub {
my $self = shift;
$self->stash($stash_key)->flush;
}
);
}
1;
__END__
=head1 NAME
Mojolicious::Plugin::Session - Session plugin for Mojolicious
=head1 SYNOPSIS
# Mojolicious::Lite
plugin session =>
{stash_key => 'mojox-session', store => 'dbi', expires_delta => 5};
# Mojolicious
$self->plugin(
session => {
stash_key => 'mojox-session',
store => 'dbi',
expires_delta => 5
}
);
=head1 DESCRIPTION
Embedded L<Mojo> sessions are recommended for using instead of this module.
L<Mojolicious::Plugin::Session> is a session plugin for L<Mojolicious>. It
creates L<MojoX::Session> instance with provided parameters, passes $tx object
before dispatch method is called and calls flush just after dispatching.
L<MojoX::Session> instance is placed in the stash.
=head1 ATTRIBUTES
L<Mojolicious::Plugin::Session> accepts all the attributes accepted by
L<MojoX::Session> and implements the following.
=head2 C<stash_key>
MojoX::Session instance will be saved in stash using this key.
=head1 SEE ALSO
L<MojoX::Session>
L<Mojolicious>
=cut