]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Manual/Workflow.pod
d860523b1dc9ebe60a8ef5c345feb517534d6a34
[maypole.git] / lib / Maypole / Manual / Workflow.pod
1 =pod
2
3 =head1 Maypole's Request Workflow
4
5 This chapter describes the progress of a request through Maypole.
6
7 An application based on C<Maypole> provides an Apache or CGI handler,
8 and eventually delivers a page. This document explains how that happens,
9 and how to influence it. We'll use the C<BeerDB> project as our example.
10 Here's a diagram that gives an overview:
11
12                               config $h
13                                   |
14                             Maypole $r
15     Apache::Request               |
16          +---- $r->get_request ---+
17         $ar                       |
18                                   |
19                           $r->parse_location
20                                   |
21                           $r->is_applicable
22                                   |
23     BeerDB::Beer        $r->call_authenticate
24        ->authenticate ------------+------------ $r->authenticate
25                                   |
26                          $r->additional_data
27                                   |
28                     $r->model_class->process($r)
29                                   |
30                      $r->view_object->process($r)
31
32 =head2 Initialize class
33
34 When the first request comes in, the application class will call its own
35 C<init> method, inherited from L<Maypole>.
36 This creates a new view object.
37
38 =head2 Construction
39
40 Once we have initialized, the handler obtains the configuration for your
41 class, and puts it into a new object. We'll call this a request
42 I<object> for the purposes of this document; it will be a new C<BeerDB>
43 object.
44
45 =head2 Getting the request
46
47 Next, the handler calls C<get_request> on the new object to have it
48 store a copy of the C<Apache::Request>. Of course, if you're not using
49 Apache, you might want to subclass this method to return something that
50 looks like an C<Apache::Request> object, and possibly also subclass the
51 next stage too to get more control over what methods are called on your
52 C<A::R>-lookalike. C<get_request> is expected to put the object in the
53 C<ar> slot of the request object.
54
55 =head2 Handling the URL
56
57 Typically, the details of the request will be passed in the URL. This is
58 done with the C<parse_location> method, which is expected to populate
59 several slots of the request object. First, C<table> and C<action>
60 should be populated with the name of the table and the action parts of
61 the URL. Any other arguments should be placed in a listref in the
62 C<args> slot, and GET and POST parameters should be arranged into a hash
63 and placed in the C<query> and C<params> slots, respectively.
64
65 Some people may not like the idea of passing everything around in the
66 URL; this is the method to override for you. Of course, you'll also need
67 to provide your own default templates to construct links using your
68 preferred format.
69
70 =head2 Is this an applicable URL?
71
72 Next, the C<is_applicable> method works out if this is actually
73 something that C<Maypole> should care about - whether the class
74 exists in the application, whether it supports the given action, and so
75 on. The action is "supported" if it exists in the model class (or its
76 ancestors) and is marked with the C<:Exported> attribute; this stops web
77 users from firing off random subroutines in your code.
78
79 This should return an Apache status code; C<OK> if the request should
80 proceed, C<DECLINED> if it should be passed on to the default handlers,
81 or whatever other codes for permissions problems. 
82
83 =head2 Are we allowed to do this?
84
85 We then look for an appropriate C<authenticate> method to call; first
86 it will try calling the C<authenticate> method of the model class, or,
87 if that does not exist, the C<authenticate> method on itself. By
88 default, this allows access to everyone for everything.
89 Your C<authenticate> methods must return an Apache status code: C<OK> or
90 C<DECLINED>. These codes are defined by the L<Maypole::Constants>
91 module, which is automatically used by your application.
92
93 =head2 Add any additional data to the request
94
95 You can write an C<additional_data> method to do any additional fiddling
96 with the request object before it is despatched. Specifically, it allows
97 you to add to the C<template_args> slot, which is a hash of arguments to
98 be added to the template, like this:
99
100     sub additional_data {
101         my $self = shift;
102         $self->{template_args}{answer} = 42;
103     }
104
105 which adds a new template variable C<answer> with the value 42.
106
107 =head2 Ask model for widget set
108
109 Asking the model class to C<process> the current request allows it to do
110 any work it needs for the given command, and populate the C<objects> and
111 C<template> slots of the request. 
112
113 The model's C<process> method is usually a thin wrapper around the
114 action that we have selected. It sets the template name to the name of
115 the action, fills C<objects> with an object of that class whose ID comes
116 from the URL arguments if there is one. For instance, C</beer/foo/12>
117 will do the moral equivalent of
118
119     $r->objects([ BeerDB::Beer->retrieve(12) ]);
120
121 Then it calls the right method: in this case, the C<foo> method with
122 the request object. This method will usually do any actions which are
123 required, including modifying the list of objects to be passed to the
124 template, or the name of the template to be called.
125
126 =head2 Ask view to process template
127
128 Now the view class has its C<process> method called. It finds the
129 appropriate templates and calls the L<Template Toolkit|Template>
130 processor.
131
132 The template processor is handed the objects, the template
133 name, and various other bits and pieces, and tries to find the right
134 template. It does this by looking first for C</beer/foo>: that is, a
135 specific template appropriate to the class. Next, it looks at
136 C</custom/foo>, a local modification, before looking for
137 C</factory/foo>, one of the default templates that came with
138 C<Maypole>.
139
140 The view puts the template's output in the C<$r-E<gt>{output}> slot. The
141 application's C<handler> method calls the C<send_output> method to push
142 it to the web server.
143
144 =head2 Default template arguments
145
146 If you're looking for the list of variables that are passed to the
147 Template Toolkit template by default, you'll find it in the
148 L<View|Maypole::Manual::View> chapter.
149
150 =head2 Links
151
152 L<Contents|Maypole::Manual>,
153 Next L<The Beer Database Revisited|Maypole::Manual::Beer>,
154 Previous
155 L<Standard Templates and Actions|Maypole::Manual::StandardTemplates>
156
157