]> git.decadent.org.uk Git - maypole.git/blob - lib/Apache/MVC.pm
Added support for extra_headers (se Maypole pod)
[maypole.git] / lib / Apache / MVC.pm
1 package Apache::MVC;
2
3 our $VERSION = '2.05';
4
5 use strict;
6 use warnings;
7
8 use base 'Maypole';
9 use mod_perl;
10
11 use constant APACHE2 => $mod_perl::VERSION >= 1.99;
12
13 if (APACHE2) {
14     require Apache2;
15     require Apache::RequestIO;
16     require Apache::RequestRec;
17     require Apache::RequestUtil;
18     require APR::URI;
19 }
20 else { require Apache }
21 require Apache::Request;
22
23 sub get_request {
24     my ( $self, $r ) = @_;
25     $self->{ar} = Apache::Request->new($r);
26 }
27
28 sub parse_location {
29     my $self = shift;
30     $self->{path} = $self->{ar}->uri;
31     my $loc = $self->{ar}->location;
32     no warnings 'uninitialized';
33     $self->{path} .= '/' if $self->{path} eq $loc;
34     $self->{path} =~ s/^($loc)?\///;
35     $self->parse_path;
36     $self->parse_args;
37 }
38
39 sub parse_args {
40     my $self = shift;
41     $self->{params} = { $self->_mod_perl_args( $self->{ar} ) };
42     $self->{query}  = { $self->_mod_perl_args( $self->{ar} ) };
43 }
44
45 sub send_output {
46     my $r = shift;
47     $r->{ar}->content_type(
48           $r->{content_type} =~ m/^text/
49         ? $r->{content_type} . "; charset=" . $r->{document_encoding}
50         : $r->{content_type}
51     );
52     $r->{ar}->headers_out->set(
53         "Content-Length" => do { use bytes; length $r->{output} }
54     );
55     foreach my $header (keys %{$r->{extra_headers}}) {
56         $r->{ar}->headers_out->set(
57             "$header" => $r->{extra_headers}{$header}
58         );
59     }
60     APACHE2 || $r->{ar}->send_http_header;
61     $r->{ar}->print( $r->{output} );
62 }
63
64 sub get_template_root {
65     my $r = shift;
66     $r->{ar}->document_root . "/" . $r->{ar}->location;
67 }
68
69 sub _mod_perl_args {
70     my ( $self, $apr ) = @_;
71     my %args;
72     foreach my $key ( $apr->param ) {
73         my @values = $apr->param($key);
74         $args{$key} = @values == 1 ? $values[0] : \@values;
75     }
76     return %args;
77 }
78
79 1;
80
81 =head1 NAME
82
83 Apache::MVC - Apache front-end to Maypole
84
85 =head1 SYNOPSIS
86
87     package BeerDB;
88     use base 'Apache::MVC';
89     BeerDB->setup("dbi:mysql:beerdb");
90     BeerDB->config->uri_base("http://your.site/");
91     BeerDB->config->display_tables([qw[beer brewery pub style]]);
92     # Now set up your database:
93     # has-a relationships
94     # untaint columns
95
96     1;
97
98 =head1 DESCRIPTION
99
100 Maypole is a Perl web application framework to Java's struts. It is 
101 essentially completely abstracted, and so doesn't know anything about
102 how to talk to the outside world. C<Apache::MVC> is a mod_perl based
103 subclass of Maypole.
104
105 To use it, you need to create a package which represents your entire
106 application. In our example above, this is the C<BeerDB> package.
107
108 This needs to first inherit from C<Apache::MVC>, and then call setup.
109 This will give your package an Apache-compatible C<handler> subroutine,
110 and then pass any parameters onto the C<setup_database> method of the
111 model class. The default model class for Maypole uses L<Class::DBI> to 
112 map a database to classes, but this can be changed by messing with the
113 configuration. (B<Before> calling setup.)
114
115 Next, you should configure your application through the C<config>
116 method. Configuration parameters at present are:
117
118 =over
119
120 =item uri_base
121
122 You B<must> specify this; it is the base URI of the application, which
123 will be used to construct links.
124
125 =item display_tables
126
127 If you do not want all of the tables in the database to be accessible,
128 then set this to a list of only the ones you want to display
129
130 =item rows_per_page
131
132 List output is paged if you set this to a positive number of rows.
133
134 =back
135
136 You should also set up relationships between your classes, such that,
137 for instance, calling C<brewery> on a C<BeerDB::Beer> object returns an
138 object representing its associated brewery.
139
140 For a full example, see the included "beer database" application.
141
142 =head1 INSTALLATION
143
144 Create a driver module like the one above.
145
146 Put the following in your Apache config:
147
148     <Location /beer>
149         SetHandler perl-script
150         PerlHandler BeerDB
151     </Location>
152
153 Copy the templates found in F<templates/factory> into the
154 F<beer/factory> directory off the web root. When the designers get
155 back to you with custom templates, they are to go in
156 F<beer/custom>. If you need to do override templates on a
157 database-table-by-table basis, put the new template in
158 F<beer/I<table>>. 
159
160 This will automatically give you C<add>, C<edit>, C<list>, C<view> and
161 C<delete> commands; for instance, a list of breweries, go to 
162
163     http://your.site/beer/brewery/list
164
165 For more information about how the system works and how to extend it,
166 see L<Maypole>.
167
168 =head1 Implementation
169
170 This class overrides a set of methods in the base Maypole class to provide it's
171 functionality. See L<Maypole> for these:
172
173 =over
174
175 =item get_request
176
177 =item get_template_root
178
179 =item parse_args
180
181 =item parse_location
182
183 =item send_output
184
185 =back
186
187 =head1 AUTHOR
188
189 Simon Cozens, C<simon@cpan.org>
190 Marcus Ramberg, C<marcus@thefeed.no>
191 Screwed up by Sebastian Riedel, C<sri@oook.de>
192
193 =head1 LICENSE
194
195 You may distribute this code under the same terms as Perl itself.