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