10 use Maypole::Constants;
12 __PACKAGE__->mk_accessors( qw( ar ) );
20 eval 'use mod_perl2; $modperl_version = $mod_perl2::VERSION; ';
22 $modperl_version = $Apache2::RequestRec::VERSION;
24 require Apache2::RequestIO;
25 require Apache2::RequestRec;
26 require Apache2::RequestUtil;
31 eval ' use mod_perl; ';
33 require Apache::Request;
42 Apache::MVC - Apache front-end to Maypole
47 use Maypole::Application;
51 A mod_perl platform driver for Maypole. Your application can inherit from
52 Apache::MVC directly, but it is recommended that you use
53 L<Maypole::Application>.
57 Create a driver module like the one illustrated in L<Maypole::Application>.
59 Put the following in your Apache config:
62 SetHandler perl-script
66 Copy the templates found in F<templates/factory> into the F<beer/factory>
67 directory off the web root. When the designers get back to you with custom
68 templates, they are to go in F<beer/custom>. If you need to override templates
69 on a database-table-by-table basis, put the new template in F<beer/I<table>>.
71 This will automatically give you C<add>, C<edit>, C<list>, C<view> and C<delete>
72 commands; for instance, to see a list of breweries, go to
74 http://your.site/beer/brewery/list
76 For more information about how the system works and how to extend it,
81 This class overrides a set of methods in the base Maypole class to provide its
82 functionality. See L<Maypole> for these:
92 my $ar = ($MODPERL2) ? $r : Apache::Request->instance($r);
103 # Reconstruct the request headers
104 $self->headers_in(Maypole::Headers->new);
106 if ($MODPERL2) { %headers = %{$self->ar->headers_in};
107 } else { %headers = $self->ar->headers_in; }
108 for (keys %headers) {
109 $self->headers_in->set($_, $headers{$_});
111 my $path = $self->ar->uri;
112 my $loc = $self->ar->location;
114 no warnings 'uninitialized';
115 $path .= '/' if $path eq $loc;
116 $path =~ s/^($loc)?\///;
130 $self->params( { $self->_mod_perl_args( $self->ar ) } );
131 $self->query( $self->params );
134 =item redirect_request
138 # FIXME: use headers_in to gather host and other information?
142 my $redirect_url = $_[0];
147 $redirect_url = $args{url};
149 my $path = $args{path} || $r->path;
150 my $host = $args{domain} || $r->ar->hostname;
151 my $protocol = $args{protocol} || $r->get_protocol;
152 $redirect_url = "${protocol}://${host}/${path}";
154 $status = $args{status} if ($args{status});
157 $r->headers_out->set('Status' => $status);
158 $r->headers_out->set('Location' => $redirect_url);
168 my $protocol = ( $self->ar->protocol =~ m/https/i ) ? 'https' : 'http' ;
178 $r->ar->content_type(
179 $r->content_type =~ m/^text/
180 ? $r->content_type . "; charset=" . $r->document_encoding
183 $r->ar->headers_out->set(
184 "Content-Length" => do { use bytes; length $r->output }
187 foreach ($r->headers_out->field_names) {
188 next if /^Content-(Type|Length)/;
189 $r->ar->headers_out->set($_ => $r->headers_out->get($_));
192 $MODPERL2 || $r->ar->send_http_header;
193 $r->ar->print( $r->output );
196 =item get_template_root
200 sub get_template_root {
202 $r->ar->document_root . "/" . $r->ar->location;
209 #########################################################
210 # private / internal methods and subs
214 my ( $self, $apr ) = @_;
216 if ($apr->isa('Apache::Request')) {
217 foreach my $key ( $apr->param ) {
218 my @values = $apr->param($key);
219 $args{$key} = @values == 1 ? $values[0] : \@values;
222 my $body = $self->_prepare_body($apr);
223 %args = %{$body->param};
229 my ( $self, $r ) = @_;
231 unless ($self->{__http_body}) {
232 my $content_type = $r->headers_in->get('Content-Type');
233 my $content_length = $r->headers_in->get('Content-Length');
234 my $body = HTTP::Body->new( $content_type, $content_length );
235 my $length = $content_length;
237 $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
238 $length -= length($buffer);
241 $self->{__http_body} = $body;
243 return $self->{__http_body};
250 Simon Cozens, C<simon@cpan.org>
255 Marcus Ramberg, C<marcus@thefeed.no>
256 Sebastian Riedel, C<sri@oook.de>
260 You may distribute this code under the same terms as Perl itself.