Apache2-Controller
view release on metacpan or search on metacpan
lib/Apache2/Controller.pm view on Meta::CPAN
=head1 RETURN VALUES
Your controller methods should use C<< eval { } >> if necessary and
act accordingly, set the right things for C<Apache2::RequestRec>
and return the right HTTP constant. See L<Apache2::Const/:http>
and L<Apache2::Controller::Refcard>.
In the event of an error, if you wish, use L<Apache2::Controller::X>
and throw one with field 'status' set to a valid HTTP return code.
This lets you implement nice error templates if your controller uses
L<Apache2::Controller::Render::Template> as a base.
See L<ERRORS> below.
Success in the controller method normally should just return the
appropriate HTTP status code. You can return HTTP_OK (200) if that
is what you mean, or it is the default status if you return OK (0).
Or, if you do C<< $self->status( Apache2::Const::HTTP_SOMETHING ) >>
and then just C<< return() >>
or return OK (0), Apache2::Controller will not override the set status.
See L<Apache2::Controller::Refcard> for a list of HTTP return constants
and corresponding numbers and messages.
=head2 REDIRECTS, ETC.
As an example of return values, take browser redirects.
There's no need for an abstraction mechanism around redirects
since you have direct access to the Apache methods.
package MyApp::Controller::Somewhere;
use base qw( Apache2::Controller Apache2::Request );
use Apache2::Const -compile => qw( REDIRECT );
# maybe dispatched from http://myapp.xyz/somewhere/go_elsewhere
sub go_elsewhere {
my ($self, @path_args) = @_;
$self->err_headers_out->add(Location => 'http://foo.bar');
return Apache2::Const::REDIRECT;
}
Keep in mind that if you use other request phase processors that
push a C<PerlLogHandler> like
L<Apache2::Controller::DBI::Connector> or
L<Apache2::Controller::Session>, those will still run,
but for example the session controller won't save the session
if you set or return an http status higher than the HTTP_OK family
(HTTP_MULTIPLE_CHOICES (300) or higher.)
You should also not fiddle with the connection by causing
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>.
Also see L<Apache2::Controller::Render::Template>.
You can use or subclass L<Apache2::Controller::X>,
to use C<< a2cx() >>,
or you can throw your own exception objects,
or just C<< die() >>, or C<< croak() >>,
or set C<< $self->status >>, headers etc., possibly printing content,
or return the appropriate status from your controller method.
See L<Apache2::Controller::X> for help on throwing exceptions
with HTTP status, data dumps, etc.
If your code does break, die or throw an exception, this is
caught by Apache2::Controller. If your controller module implements
an C<<error() >> method,
then C<< $handler->error() >> will be called passing the C<< $EVAL_ERROR >>
or exception object as the first argument.
package MyApp::C::Foo;
use YAML::Syck;
# ...
sub error {
my ($self, $X) = @_;
$self->status( Apache2::Const::HTTP_BAD_REQUEST );
$self->content_type('text/plain');
$self->print("Take this job and shove it!\n", "\n", $X, "\n");
if ($X->isa('Apache2::Controller::X')) {
# usually you wouldn't show gory details to the user...
$self->print(Dump($X->dump)) if $X->dump;
$self->print($X->trace) if $X->trace;
}
}
For instance
L<Apache2::Controller::Render::Template> implements
C<< error() >> for you, which looks for
the appropriately named error template as
F<template_dir/errors/###.html>.
Of course, all exceptions are sent to the error log using
L<Log::Log4perl> DEBUG() before the handler completes, and
any refusal status greater or equal to 400 (HTTP_BAD_REQUEST)
will be written to the access log with L<Apache2::Log> log_reason()
using the first few characters of the error.
See L<Apache2::Controller::Session/ERRORS> for how to control
whether or not a session is saved. Usually it is automatically
saved, but not if you have an error.
C<< error() >> does not have to roll back DBI handles if you
lib/Apache2/Controller.pm view on Meta::CPAN
You deploy the same Apache, the
same CPAN modules and your whole application package to every server,
and attach a url-generating subroutine to the L<Template|Template> stash
that puts in a different hostname when the URI is one of your
load-intensive functions.
<a href="[% myurl('/easy') %]">easy</a> "http://pony.x.y/easy"
<a href="[% myurl('/hard') %]">hard</a> "http://packhorse.x.y/hard"
Web designers can be taught to use this function C<< myurl() >>,
but system admins
maintain the map that it loads to figure out what servers to use.
Then the Apache2 config files on those
packhorse servers would pre-load only the subclassed controllers
that you needed, and redirect all other uri requests to the pony servers.
=cut
use strict;
use warnings FATAL => 'all';
use English '-no_match_vars';
use base qw( Apache2::Controller::Methods );
use Readonly;
use Scalar::Util qw( blessed );
use Log::Log4perl qw(:easy);
use YAML::Syck;
use Digest::SHA qw( sha224_base64 );
use URI;
use HTTP::Status qw( status_message );
use Scalar::Util qw( looks_like_number );
use Apache2::Controller::X;
use Apache2::Controller::Funk qw( log_bad_request_reason );
use Apache2::Request;
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::RequestUtil ();
use Apache2::Log;
use Apache2::Const -compile => qw( :common :http );
=head1 FUNCTIONS
=head2 a2c_new
$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 );
sub a2c_new {
my ($class, $r) = @_;
return SUPER::new(
$class, $r,
POST_MAX => 65_535,
TEMP_DIR => '/dev/shm',
);
# $self is already blessed in the class hierarchy
}
package MyApp::Controller::SomeURI;
use base qw( MyApp::ControllerBase );
sub allowed_methods qw( uri_one uri_two );
sub uri_one { # ...
If you need to do the same stuff every time a request
starts, you can override the constructor through a
class hierarchy.
package MyApp::ControllerBase;
use base qw( Apache2::Controller Apache2::Request );
sub new {
my ($class, $r, @apr_override_args) = @_;
my $self = SUPER::new(
$class, $r,
POST_MAX => 65_535,
TEMP_DIR => '/dev/shm',
@apr_override_args,
);
# $self is already blessed in the class hierarchy
# do request-startup stuff common to all controller modules
return $self;
}
package MyApp::Controller::SomeURI;
use base qw( MyApp::ControllerBase );
sub allowed_methods qw( uri_one uri_two );
sub new {
my ($class, $r) = @_;
my $self = SUPER::a2c_new(
$class, $r,
);
# no need to bless, A2C blesses into the child class
( run in 2.757 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )