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