]> git.decadent.org.uk Git - maypole.git/blob - lib/Apache2/MVC.pm
Apache2::MVC, Maypole::Application, parse_args() and much more...
[maypole.git] / lib / Apache2 / MVC.pm
1 package Apache2::MVC;
2
3 use base qw(Apache::MVC::Base Maypole);
4 use Apache2;
5 use Apache::RequestRec;
6 use Apache::RequestUtil;
7 use Apache::Request;
8 use APR::URI;
9 use strict;
10 use warnings;
11
12 our $VERSION = "0.1";
13
14 sub get_request {
15     my ( $self, $r ) = @_;
16     $self->{ar} = Apache::Request->new($r);
17 }
18
19 sub parse_args {
20     my $self = shift;
21     $self->{params} = { $self->_mod_perl_args( $self->{ar} ) };
22     $self->{query}  = { $self->_mod_perl_args( $self->{ar} ) };
23 }
24
25 sub _mod_perl_args {
26     my ( $self, $apr ) = @_;
27     my %args;
28     foreach my $key ( $apr->param ) {
29         my @values = $apr->param($key);
30         $args{$key} = @values == 1 ? $values[0] : \@values;
31     }
32     return %args;
33 }
34
35 1;
36
37 =head1 NAME
38
39 Apache2::MVC - Apache2 front-end to Maypole
40
41 =head1 SYNOPSIS
42
43     package BeerDB;
44     use base 'Apache::MVC';
45     BeerDB->setup("dbi:mysql:beerdb");
46     BeerDB->config->{uri_base} = "http://your.site/";
47     BeerDB->config->{display_tables} = [qw[beer brewery pub style]];
48     # Now set up your database:
49     # has-a relationships
50     # untaint columns
51
52     1;
53
54 =head1 DESCRIPTION
55
56 Maypole is a Perl web application framework to Java's struts. It is 
57 essentially completely abstracted, and so doesn't know anything about
58 how to talk to the outside world. C<Apache2::MVC> is a mod_perl2 based
59 subclass of Maypole.
60
61 To use it, you need to create a package which represents your entire
62 application. In our example above, this is the C<BeerDB> package.
63
64 This needs to first inherit from C<Apache2::MVC>, and then call setup.
65 This will give your package an Apache-compatible C<handler> subroutine,
66 and then pass any parameters onto the C<setup_database> method of the
67 model class. The default model class for Maypole uses L<Class::DBI> to 
68 map a database to classes, but this can be changed by messing with the
69 configuration. (B<Before> calling setup.)
70
71 Next, you should configure your application through the C<config>
72 method. Configuration parameters at present are:
73
74 =over
75
76 =item uri_base
77
78 You B<must> specify this; it is the base URI of the application, which
79 will be used to construct links.
80
81 =item display_tables
82
83 If you do not want all of the tables in the database to be accessible,
84 then set this to a list of only the ones you want to display
85
86 =item rows_per_page
87
88 List output is paged if you set this to a positive number of rows.
89
90 =back
91
92 You should also set up relationships between your classes, such that,
93 for instance, calling C<brewery> on a C<BeerDB::Beer> object returns an
94 object representing its associated brewery.
95
96 For a full example, see the included "beer database" application.
97
98 =head1 INSTALLATION
99
100 Create a driver module like the one above.
101
102 Put the following in your Apache config:
103
104     <Location /beer>
105         SetHandler perl-script
106         PerlHandler BeerDB
107     </Location>
108
109 Copy the templates found in F<templates/factory> into the
110 F<beer/factory> directory off the web root. When the designers get
111 back to you with custom templates, they are to go in
112 F<beer/custom>. If you need to do override templates on a
113 database-table-by-table basis, put the new template in
114 F<beer/I<table>>. 
115
116 This will automatically give you C<add>, C<edit>, C<list>, C<view> and
117 C<delete> commands; for instance, a list of breweries, go to 
118
119     http://your.site/beer/brewery/list
120
121 For more information about how the system works and how to extend it,
122 see L<Maypole>.
123
124 =head1 AUTHOR
125
126 Simon Cozens, C<simon@cpan.org>
127 Marcus Ramberg, C<marcus@thefeed.no>
128 Screwed up by Sebastian Riedel, C<sri@oook.de>
129
130 =head1 LICENSE
131
132 You may distribute this code under the same terms as Perl itself.