Apache2-Controller
view release on metacpan or search on metacpan
lib/Apache2/Controller/Session.pm view on Meta::CPAN
in cookies, you should roll your own handler with L<Apache2::Cookie>.
Your session module uses an Apache2::Controller::Session tracker module
as a base and you specify your L<Apache::Session> options either as
config variables or by implementing a method C<<getoptions()>>.
Instead of having a bunch of different options for all the different
L<Apache::Session> types, it's easier for me to make you provide
a method C<session_options()> in your subclass that will return a
has of the appropriate options for your chosen session store.
=head2 CONFIG ALTERNATIVE 1: directives or PerlSetVar variables
If you do not implement a special C<getoptions()> method
or use settings other than these, these are the default:
<Location /elsewhere>
PerlHeaderParserHandler MyApp::ApacheSessionFile
A2C_Session_Class Apache::Session::File
A2C_Session_Opts Directory /tmp/sessions
A2C_Session_Opts LockDirectory /var/lock/sessions
</Location>
Until directives work and the kludgey PerlSetVar syntax goes away,
spaces are not allowed in the argument values. Warning!
The kludgey PerlSetVar syntax will go away when
directives work properly.
=head2 CONFIG ALTERNATIVE 2: C<< YourApp::YourSessionClass->get_options() >>
Implement C<get_options()> in your subclass to return the final options
hashref for your L<Apache::Session> session type.
For example, if your app uses DBIx::Class, maybe you want to
go ahead and init your schema so you can get the database
handle directly and pass that to your session class.
See
L<Apache2::Controller::DBI::Connector|Apache2::Controller::DBI::Connector>
for directives to set database connection in pnotes->{a2c}{dbh}.
Here's a code example for Location /somewhere above:
package MyApp::Session;
use strict;
use warnings FATAL => 'all';
use base qw( Apache2::Controller::Session::Cookie );
use English '-no_match_vars';
use Apache2::Controller::X;
sub get_options {
my ($self) = @_;
my $r = $self->{r};
eval {
$r->pnotes->{a2c}{dbh} ||= DBI->connect(
'dbi:mysql:database=myapp;host=mydbhost';
'myuser', 'mypassword'
);
};
a2cx "cannot connect to DB: $EVAL_ERROR" if $EVAL_ERROR;
my $dbh = $r->pnotes->{a2c}{dbh}; # save handle for later use
# in controllers, etc.
return {
Handle => $dbh,
LockHandle => $dbh,
};
}
If you do it this way or use Apache::DBI,
be careful about transactions. See L<DATABASE TRANSACTION SAFETY> below.
# ...
In your controller module, access the session in C<< pnotes->{a2c}{session} >>.
package MyApp::Controller::SomeWhere::Overtherainbow;
use base qw( Apache2::Controller Apache2::Request );
# ...
sub default {
my ($self) = @_;
my $session = $self->pnotes->{a2c}{session};
$session->{foo} = 'bar';
# session will be saved by a PerlLogHandler
# that was automatically pushed by Apache2::Controller::Session
# and in my example
return Apache2::Const::HTTP_OK;
}
=head1 DATABASE TRANSACTION SAFETY
When this handler runs, it ties the session into a special
hash that it keeps internally, and loads a copy into
C<< $r->pnotes->{a2c}{session} >>. So, modifying the session hash
is fine, as long as you do not dereference it, or as long
as you save your changes back to C<< $r->pnotes->{a2c}{session} >>.
No changes are auto-committed. The one in pnotes is
copied back into the tied session hash in a C<PerlLogHandler>,
after the server finishes output but I<before> it closes
the connection to the client. If the connection is detected
to be aborted in the C<PerlLogHandler> phase, changes are NOT
saved into the session object.
If you implemented C<get_options()> as per above and decided
to save your $dbh for later use in your controllers, feel free
to start transactions and use them normally. Just make sure you
use L<perlfunc/eval> correctly and roll back or commit your
transactions.
If you decide to push a C<PerlLogHandler>
to roll back transactions for broken connections or something,
( run in 0.597 second using v1.01-cache-2.11-cpan-39bf76dae61 )