Apache2-Controller
view release on metacpan or search on metacpan
lib/Apache2/Controller.pm view on Meta::CPAN
of closures that access variables in your controller subclass
package space, which are cached by modperl in child processes
across independent web requests. Be careful with that and use
Devel::Size to keep memory usage down. I have no idea how this
would work under threaded mpm.
=head1 CONTENT TYPE
Your controller should set content type with C<< $self->content_type() >>
to something specific if you need that. Otherwise it will let
mod_perl set it to whatever it chooses when you start to print.
This is usually text/html.
=head1 LOGGING
Apache2::Controller uses L<Log::Log4perl>. See that module
for information on how to set up a format file or statement.
For example, in a perl startup script called at Apache2 start time,
do something like:
use Log::Log4perl;
log4perl.rootLogger=DEBUG, LogFile
log4perl.appender.LogFile=Log::Log4perl::Appender::File
log4perl.appender.LogFile.filename=/var/log/mysite_error_log
log4perl.appender.LogFile.layout=PatternLayout
log4perl.appender.LogFile.layout.ConversionPattern=%M [%L]: %m%n
};
Log::Log4perl->init(\$logconf);
These settings will be cloned to every modperl child on fork.
=head1 MVC
Apache2::Controller provides the controller, mainly.
L<Apache2::Controller::Render::Template> is one example
of a view that can be used as a second base with
C<use base> in your controller module. As for the Model
part of Model-View-Controller, Apache2::Controller leaves
that entirely up to you and does not force you to
wrap anything in an abstraction class.
The C<handler()> subroutine is in your base class and your
controller modules will be running from memory in the mod_perl
child interpreter. So,
you can use package namespace effectively to store data
that will persist in the mod_perl child across requests.
=head1 LOAD BALANCING
A2C does not have to load
all site modules for every page handler, which could help with load-balancing
highly optimized handlers for specific URI's while having a universal
application installer.
Picture if you will, a programming utopia in which all engineers
are respected, highly paid and content, and managers make
correct decisions to rely on open-source software.
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.
lib/Apache2/Controller.pm view on Meta::CPAN
sub uri_one {
my ($self) = @_;
$self->content_type('image/gif');
# ...
return Apache2::Const::HTTP_OK;
}
sub uri_two { # ...
Similarly, to do something always at the end of every
request, from within the dispatched PerlResponseHandler:
package MyApp::Controller::SomeURI;
use Devel::Size;
use Log::Log4perl qw(:easy);
my $MAX = 40 * 1024 * 1024;
sub DESTROY {
my ($self) = @_;
my $size = total_size($self); # whoo baby!
INFO("size of $self->{class} is bigger than $MAX!") if $size > $MAX;
return; # self is destroyed
}
See L<USING INHERITANCE> below for more tips.
=cut
my %temp_dirs = ( );
my %post_maxes = ( );
sub a2c_new {
my ($class, $r, @apr_opts) = @_;
DEBUG sub {
"new $class, reqrec is '$r', apr_opts:\n".Dump(\@apr_opts)
};
my $self = {
class => $class,
};
bless $self, $class;
DEBUG "creating Apache2::Request object";
my $req = Apache2::Request->new( $r, @apr_opts );
DEBUG "request object is '$req'";
$self->{r} = $req; # for Apache2::Request subclass automagic
my $pnotes_a2c = $req->pnotes->{a2c} || { };
my $method = $pnotes_a2c->{method};
$self->{method} = $method;
$self->{path_args} = $pnotes_a2c->{path_args};
# don't instantiate the 'session' key of $self unless it's implemented
# in some earlier stage of the apache lifecycle.
my $session = $pnotes_a2c->{session};
if ($session) {
$self->{session} = $session;
DEBUG(sub{"found and attached session to controller self:\n".Dump($session)});
# this is the same reference as the pnotes reference still,
# so the cleanup handler will find all changes made to it
}
DEBUG sub { Dump({
# for simple debugging, stringify objects, otherwise this can get huge
map {($_ => defined $self->{$_} ? "$self->{$_}" : undef)} keys %$self
}) };
return $self;
}
=head1 METHODS
Methods are also extended by other modules in the A2C family.
See L<Apache2::Controller::Methods>.
=head2 handler
# called from Apache, your subclass pushed to PerlResponseHandler
# by your A2C dispatch handler:
MyApp::Controller::Foo->handler( Apache2::RequestRec object )
The handler is pushed from an Apache2::Controller::Dispatch
subclass and via your dispatched subclass of Apache2::Controller.
It should not be set in the config file. It looks
for the controller module name in C<< $r->pnotes->{a2c}{controller} >>
and for the method name in C<< $r->pnotes->{a2c}{method} >>.
Errors are intercepted and if the handler object was created
and implements an C<< $handler->error($exception) >> method
then the exception will be passed as the argument.
An HTTP status code of HTTP_BAD_REQUEST or greater will
cause log_reason to be called with a truncated error string
and the uri for recording in the access log.
=cut
my %supports_error_method = ( );
sub handler : method {
my ($class, $r) = @_;
return $class if !defined $r;
my $pnotes_a2c = $r->pnotes->{a2c} || { };
my $method = $pnotes_a2c->{method};
DEBUG("$class -> $method");
my ($handler, $status, $X, $used_error_method_successfully) = ( );
eval {
$handler = $class->a2c_new($r);
$method = $handler->{method};
DEBUG("executing $class -> $method()");
my $args = $pnotes_a2c->{path_args} || [];
$status = $handler->$method(@{$args});
( run in 0.768 second using v1.01-cache-2.11-cpan-e1769b4cff6 )