13 use Maypole::Constants;
15 __PACKAGE__->mk_accessors( qw( ar ) );
21 $MODPERL2 = ( exists $ENV{MOD_PERL_API_VERSION} and
22 $ENV{MOD_PERL_API_VERSION} >= 2 );
24 eval 'use mod_perl2; $modperl_version = $mod_perl2::VERSION;';
26 $modperl_version = $Apache2::RequestRec::VERSION;
28 require Apache2::RequestIO;
29 require Apache2::RequestRec;
30 require Apache2::RequestUtil;
31 eval 'use Apache2::Const -compile => qw/REDIRECT/;'; # -compile 4 no import
35 eval ' use mod_perl; ';
37 require Apache::Request;
38 eval 'use Apache::Constants -compile => qw/REDIRECT/;';
46 Apache::MVC - Apache front-end to Maypole
51 use Maypole::Application;
55 A mod_perl platform driver for Maypole. Your application can inherit from
56 Apache::MVC directly, but it is recommended that you use
57 L<Maypole::Application>.
61 Create a driver module like the one illustrated in L<Maypole::Application>.
63 Put the following in your Apache config:
66 SetHandler perl-script
70 Copy the templates found in F<templates/factory> into the F<beer/factory>
71 directory off the web root. When the designers get back to you with custom
72 templates, they are to go in F<beer/custom>. If you need to override templates
73 on a database-table-by-table basis, put the new template in F<beer/I<table>>.
75 This will automatically give you C<add>, C<edit>, C<list>, C<view> and C<delete>
76 commands; for instance, to see a list of breweries, go to
78 http://your.site/beer/brewery/list
80 For more information about how the system works and how to extend it,
85 This class overrides a set of methods in the base Maypole class to provide its
86 functionality. See L<Maypole> for these:
98 $ar = eval {require Apache2::Request} ? Apache2::Request->new($r) : $r;
100 else { $ar = Apache::Request->instance($r); }
111 # Reconstruct the request headers
112 $self->headers_in(Maypole::Headers->new);
114 if ($MODPERL2) { %headers = %{$self->ar->headers_in};
115 } else { %headers = $self->ar->headers_in; }
116 for (keys %headers) {
117 $self->headers_in->set($_, $headers{$_});
119 my $path = $self->ar->uri;
120 my $loc = $self->ar->location;
122 no warnings 'uninitialized';
123 $path .= '/' if $path eq $loc;
124 $path =~ s/^($loc)?\///;
137 $self->params( { $self->_mod_perl_args( $self->ar ) } );
138 $self->query( $self->params );
141 =item redirect_request
148 my $redirect_url = $_[0];
149 my $status = $MODPERL2 ? eval 'Apache2::Const::REDIRECT;' :
150 eval 'Apache::Constants::REDIRECT;'; # why have to eval this?
154 $redirect_url = $args{url};
156 my $path = $args{path} || $r->path;
157 my $host = $args{domain} || $r->ar->hostname;
158 my $protocol = $args{protocol} || $r->get_protocol;
159 $redirect_url = "${protocol}://${host}/${path}";
161 $status = $args{status} if ($args{status});
164 $r->ar->status($status);
165 $r->ar->headers_out->set('Location' => $redirect_url);
175 my $protocol = ( $self->ar->protocol =~ m/https/i ) ? 'https' : 'http' ;
185 $r->ar->content_type(
186 $r->content_type =~ m/^text/
187 ? $r->content_type . "; charset=" . $r->document_encoding
190 $r->ar->headers_out->set(
191 "Content-Length" => do { use bytes; length $r->output }
194 foreach ($r->headers_out->field_names) {
195 next if /^Content-(Type|Length)/;
196 $r->ar->headers_out->set($_ => $r->headers_out->get($_));
199 $MODPERL2 || $r->ar->send_http_header;
200 $r->ar->print( $r->output );
203 =item get_template_root
207 sub get_template_root {
209 $r->ar->document_root . "/" . $r->ar->location;
216 #########################################################
217 # private / internal methods and subs
221 my ( $self, $apr ) = @_;
223 if ($apr->isa('Apache::Request')) {
224 foreach my $key ( $apr->param ) {
225 my @values = $apr->param($key);
226 $args{$key} = @values == 1 ? $values[0] : \@values;
229 my $body = $self->_prepare_body($apr);
230 %args = %{$body->param};
231 my $uri = URI->new($self->ar->unparsed_uri);
232 foreach my $key ($uri->query_param) {
233 if (ref $args{$key}) {
234 push (@{$args{$key}}, $uri->query_param($key));
237 $args{$key} = [ $args{$key}, $uri->query_param($key) ];
239 my @args = $uri->query_param($key);
240 if (scalar @args > 1) {
241 $args{$key} = [ $uri->query_param($key) ];
243 $args{$key} = $uri->query_param($key);
253 my ( $self, $r ) = @_;
255 unless ($self->{__http_body}) {
256 my $content_type = $r->headers_in->get('Content-Type');
257 my $content_length = $r->headers_in->get('Content-Length');
258 my $body = HTTP::Body->new( $content_type, $content_length );
259 my $length = $content_length;
261 $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
262 $length -= length($buffer);
265 $self->{__http_body} = $body;
267 return $self->{__http_body};
274 Simon Cozens, C<simon@cpan.org>
279 Marcus Ramberg, C<marcus@thefeed.no>
280 Sebastian Riedel, C<sri@oook.de>
284 You may distribute this code under the same terms as Perl itself.