-
-
18 Apr 2008 10:27:38 UTC
- Distribution: Maypole
- Source (raw)
- Browse (raw)
- Changes
- How to Contribute
- Issues (13)
- Testers (225 / 231 / 1)
- Kwalitee
Bus factor: 0- 65.31% Coverage
- License: unknown
- Activity
24 month- Tools
- Download (149.99KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
- Dependencies
- CGI::Simple
- CGI::Untaint
- CGI::Untaint::date
- CGI::Untaint::email
- Class::DBI
- Class::DBI::AbstractSearch
- Class::DBI::Loader
- Class::DBI::Loader::Relationship
- Class::DBI::Pager
- Class::DBI::Plugin::RetrieveAll
- Class::DBI::Plugin::Type
- Digest::MD5
- File::MMagic::XS
- HTML::Tree
- HTTP::Body
- Template
- Template::Plugin::Class
- Test::MockModule
- UNIVERSAL::moniker
- UNIVERSAL::require
- URI
- and possibly others
- Reverse dependencies
- CPAN Testers List
- Dependency graph
NAME
Maypole::Manual::Workflow - Maypole's Request Workflow
DESCRIPTION
This chapter describes the progress of a request through Maypole.
An application based on
Maypole
provides an Apache or CGI handler, and eventually delivers a page. This document explains how that happens, and how to influence it. We'll use theBeerDB
project as our example. Here's a diagram that gives an overview:config $h | Maypole $r Apache::Request | +---- $r->get_request ---+ $ar | | $r->parse_location | $r->is_applicable | BeerDB::Beer $r->call_authenticate ->authenticate ------------+------------ $r->authenticate | $r->additional_data | $r->model_class->process($r) | $r->view_object->process($r)
Initialize class
When the first request comes in, the application class will call its own
init
method, inherited from Maypole. This creates a new view object.Construction
Once we have initialized, the handler obtains the configuration for your class, and puts it into a new object. We'll call this a request object for the purposes of this document; it will be a new
BeerDB
object.Getting the request
Next, the handler calls
get_request
on the new object to have it store a copy of theApache::Request
. Of course, if you're not using Apache, you might want to subclass this method to return something that looks like anApache::Request
object, and possibly also subclass the next stage too to get more control over what methods are called on yourA::R
-lookalike.get_request
is expected to put the object in thear
slot of the request object.Handling the URL
Typically, the details of the request will be passed in the URL. This is done with the
parse_location
method, which is expected to populate several slots of the request object. First,table
andaction
should be populated with the name of the table and the action parts of the URL. Any other arguments should be placed in a listref in theargs
slot, and GET and POST parameters should be arranged into a hash and placed in thequery
andparams
slots, respectively.Some people may not like the idea of passing everything around in the URL; this is the method to override for you. Of course, you'll also need to provide your own default templates to construct links using your preferred format.
Is this an applicable URL?
Next, the
is_applicable
method works out if this is actually something thatMaypole
should care about - whether the class exists in the application, whether it supports the given action, and so on. The action is "supported" if it exists in the model class (or its ancestors) and is marked with the:Exported
attribute; this stops web users from firing off random subroutines in your code.This should return an Apache status code;
OK
if the request should proceed,DECLINED
if it should be passed on to the default handlers, or whatever other codes for permissions problems.Are we allowed to do this?
We then look for an appropriate
authenticate
method to call; first it will try calling theauthenticate
method of the model class, or, if that does not exist, theauthenticate
method on itself. By default, this allows access to everyone for everything. Yourauthenticate
methods must return an Apache status code:OK
orDECLINED
. These codes are defined by the Maypole::Constants module, which is automatically used by your application.Add any additional data to the request
You can write an
additional_data
method to do any additional fiddling with the request object before it is despatched. Specifically, it allows you to add to thetemplate_args
slot, which is a hash of arguments to be added to the template, like this:sub additional_data { my $self = shift; $self->{template_args}{answer} = 42; }
which adds a new template variable
answer
with the value 42.Ask model for widget set
Asking the model class to
process
the current request allows it to do any work it needs for the given command, and populate theobjects
andtemplate
slots of the request.The model's
process
method is usually a thin wrapper around the action that we have selected. It sets the template name to the name of the action, fillsobjects
with an object of that class whose ID comes from the URL arguments if there is one. For instance,/beer/foo/12
will do the moral equivalent of$r->objects([ BeerDB::Beer->retrieve(12) ]);
Then it calls the right method: in this case, the
foo
method with the request object. This method will usually do any actions which are required, including modifying the list of objects to be passed to the template, or the name of the template to be called.Ask view to process template
Now the view class has its
process
method called. It finds the appropriate templates and calls the Template Toolkit processor.The template processor is handed the objects, the template name, and various other bits and pieces, and tries to find the right template. It does this by looking first for
/beer/foo
: that is, a specific template appropriate to the class. Next, it looks at/custom/foo
, a local modification, before looking for/factory/foo
, one of the default templates that came withMaypole
.The view puts the template's output in the
$r->{output}
slot. The application'shandler
method calls thesend_output
method to push it to the web server.Default template arguments
If you're looking for the list of variables that are passed to the Template Toolkit template by default, you'll find it in the View chapter.
Links
Contents, Next The Beer Database Revisited, Previous Standard Templates and Actions
Module Install Instructions
To install Maypole, copy and paste the appropriate command in to your terminal.
cpanm Maypole
perl -MCPAN -e shell install Maypole
For more information on module installation, please visit the detailed CPAN module installation guide.