1 package Plack::Middleware::Session;
6 our $AUTHORITY = 'cpan:STEVAN';
14 use parent 'Plack::Middleware';
16 use Plack::Util::Accessor qw(
25 $self->session_class( 'Plack::Session' ) unless $self->session_class;
26 $self->state( 'Cookie' ) unless $self->state;
28 $self->state( $self->inflate_backend('Plack::Session::State', $self->state) );
29 $self->store( $self->inflate_backend('Plack::Session::Store', $self->store) );
33 my($self, $prefix, $backend) = @_;
35 return $backend if defined $backend && Scalar::Util::blessed $backend;
38 push @class, $backend if defined $backend; # undef means the root class
41 Plack::Util::load_class(@class)->new();
48 $env->{'plack.session'} = $self->session_class->new(
49 state => $self->state,
50 store => $self->store,
51 request => Plack::Request->new( $env )
54 my $res = $self->app->($env);
55 $self->response_cb($res, sub {
56 my $res = Plack::Response->new(@{$_[0]});
57 $env->{'plack.session'}->finalize( $res );
58 @{$_[0]} = @{$res->finalize};
70 Plack::Middleware::Session - Middleware for session management
80 [ 'Content-Type' => 'text/plain' ],
81 [ 'Hello, your Session ID is ' . $env->{'plack.session'}->id ]
90 # Or, use the File store backend (great if you use multiprocess server)
91 # For more options, see perldoc Plack::Session::Store::File
93 enable 'Session', store => 'File';
99 This is a Plack Middleware component for session management. By
100 default it will use cookies to keep session state and store data in
101 memory. This distribution also comes with other state and store
102 solutions. See perldoc for these backends how to use them.
104 It should be noted that we store the current session in the
105 C<plack.session> key inside the C<$env> where you can access it
106 as needed. Additionally, as of version 0.09, you can call the
107 C<session> method of a L<Plack::Request> instance to fetch
108 whatever is stored in C<plack.session>.
114 =item L<Plack::Session::State>
116 This will maintain session state by passing the session through
117 the request params. It does not do this automatically though,
118 you are responsible for passing the session param.
120 =item L<Plack::Session::State::Cookie>
122 This will maintain session state using browser cookies.
130 =item L<Plack::Session::Store>
132 This is your basic in-memory session data store. It is volatile storage
133 and not recommended for multiprocessing environments. However it is
134 very useful for development and testing.
136 =item L<Plack::Session::Store::File>
138 This will persist session data in a file. By default it uses
139 L<Storable> but it can be configured to have a custom serializer and
142 =item L<Plack::Session::Store::Cache>
144 This will persist session data using the L<Cache> interface.
146 =item L<Plack::Session::Store::Null>
148 Sometimes you don't care about storing session data, in that case
149 you can use this noop module.
155 The following are options that can be passed to this mdoule.
161 This is expected to be an instance of L<Plack::Session::State> or an
162 object that implements the same interface. If no option is provided
163 the default L<Plack::Session::State::Cookie> will be used.
167 This is expected to be an instance of L<Plack::Session::Store> or an
168 object that implements the same interface. If no option is provided
169 the default L<Plack::Session::Store> will be used.
171 It should be noted that this default is an in-memory volatile store
172 is only suitable for development (or single process servers). For a
173 more robust solution see L<Plack::Session::Store::File> or
174 L<Plack::Session::Store::Cache>.
176 =item I<session_class>
178 This can be used to override the actual session class. It currently
179 defaults to L<Plack::Session> but you can substitute any class which
180 implements the same interface.
186 All complex software has bugs lurking in it, and this module is no
187 exception. If you find a bug please either email me, or add the bug
194 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
196 =head1 COPYRIGHT AND LICENSE
198 Copyright 2009 Infinity Interactive, Inc.
200 L<http://www.iinteractive.com>
202 This library is free software; you can redistribute it and/or modify
203 it under the same terms as Perl itself.