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