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