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