Apache2-Controller
view release on metacpan or search on metacpan
lib/Apache2/Controller.pm view on Meta::CPAN
Apache2 to close it prematurely, else the post-response handlers
may not run or won't run synchronously before another request
is received that may have depended on their behavior.
(For example, you can't use a C<< PerlCleanupHandler >>
to do things like that because the request has already closed,
and it doesn't get processed before taking in the next request,
even when running in single-process mode.)
=head1 ERRORS
If you decide to set an error status code, you can print your
own content and return that status code.
If you want to use error templates,
barf L<Apache2::Controller::X> objects. These print a stack trace
to the error log at the WARN level of L<Log::Log4perl> from
this module's namespace. If errors crop up from
other A2C request phase handlers, try setting
WARN log level for L<Apache2::Controller::NonResponseBase>
or L<Apache2::Controller::NonResponseRequest>.
lib/Apache2/Controller.pm view on Meta::CPAN
$handler = MyApp::C::ControllerSubclass->a2c_new( Apache2::RequestRec object )
This is called by handler() to create the Apache2::Controller object
via the module chosen by your L<Apache2::Controller::Dispatch> subclass.
We use C<< a2c_new >> instead of the conventional C<< new >>
because, in case you want to suck in the L<Apache2::Request>
methods with that module's automagic, then you don't get
confused about how C<<SUPER::>> behaves. Otherwise you
get into a mess of keeping track of the order of bases
so you don't call C<< Apache2::Request->new() >> by accident,
which breaks everything.
=head3 subclassing C<a2c_new()>
To set params for the L<Apache2::Request> object,
you have to subclass C<a2c_new()>.
package MyApp::ControllerBase;
use base qw( Apache2::Controller Apache2::Request );
lib/Apache2/Controller/Session.pm view on Meta::CPAN
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,
or C<PerlCleanupHandler> to do something else (don't use
post-connection phases for database transactions or you'll get out of sync),
be aware
that this handler 'unshifts' a log handler closure that
saves the copy in pnotes back into the tied hash.
It does this by re-ordering the C<PerlLogHandler> stack with
L<Apache2::RequestUtil/get_handlers> and C<set_handlers()>.
So if you push another post-response handler that wants to
choose whether to save the session or not, be aware that
it may not work as you expect unless you re-order that
phase's handler stack again.
=head1 TO SAVE OR NOT TO SAVE
Generally in your code, it's complicated to decide whether everything
has worked before you save anything to the session. It's easier just
to save stuff, and then if something goes wrong, it is as if this
rolls back.
A C<PerlLogHandler> subroutine is 'unshifted' to the request stack
which decides whether to save changes to the session. By default,
it saves changes only if A) the connection is not aborted,
and B) your controller set HTTP status < 300,
i.e. it returned C<OK> (0), one of the C<HTTP_CONTINUE> family (100+)
or one of the C<HTTP_OK> family (200+).
So for an C<HTTP_SERVER_ERROR>, or throwing an exception, redirecting,
forbidding access, etc (>= 300), it normally would not save changes.
If your L<Apache2::Controller> controller module returns one of these
non-OK statuses, but you want to force the saving of the session contents,
set C<< $self->pnotes->{a2c}{session_force_save} = 1 >> before
( run in 0.527 second using v1.01-cache-2.11-cpan-de7293f3b23 )