Apache::TieRod

TieRod - Jakarta Struts-like MVC Framework in Perl


NAME

TieRod - Jakarta Struts-like MVC Framework in Perl


DESCRIPTION

TieRod is an attempt to provide a Web Application Framework that derives much of its features from the Apache Software Foundation's Jakarta Struts project.


OVERVIEW

In poking around, I realized that I like the way Struts gives the developer control over many separate pieces of a single request. To this end, armed with just RequestProcessor.java, I began to replicate this in perl. This scheme allows one to create very simple or complex paths through which a single request will go, bypassing certain pieces here and extending them there. Much like Apache's request phases, various things can happen at any step of the process. So, it seems like this methodology fits well, and a set of sub phases to the content handling of a request.

TieRod is built upon Apache/mod_perl. At its core, it is essentially an Apache handler that delegates specific tasks out to particular modules in a step by step process. The main ``controller'' of the system is TieRod::ActionHandler, which is analogous to Struts' ActionServlet, is where http requests begin their journey, processed by the configured RequestProcessor (TieRod::RequestProcessor being the default), and are processed according to their path's configuration, until some result is executed. In general, a path is associated with an Action, and these Actions can be simple or complex depending on the requirements of the intended view. A path may also have an ActionForm to process and validate any input.

TieRod is path-based. Actions are executed based on the path info starting at the top level to which the ActionHandler is configured to handle. When the ActionHandler is handed control by Apache, it sets up some stuff related to the request (session, cookies, etc.) and then hands control to the RequestHandler, which checks the path and figures out what to do from there. The configuration can tell the RequestHandler to redirect somewhere else, display a static page, verify parameter and perform some actions, etc.

Template Toolkit is currently the only supported view mechanism. When the RequestProcessor finally completes all of the configured steps for the request, it will hand control over to the configured template (if the path is, in fact, a template) including all of the data has has been added to the sessions data hash.


ACTIONHANDLER

TieRod::ActionHandler is the top level class that is responsible for setting up several things that most, if not all, requests will need such as the TieRod::Request object, some configuration objects, cookies, and the session. It is the controller in the MVC world.


REQUESTPROCESSOR

TieRod::RequestProcessor is the meat of the framework. Depending on a path's configuration, it may exexcute one or more of about 14 steps before handing control to the template. This, I believe, is what gives the framework its edge. The RequestProcessor is also one of the parts of the framework that *should* be sufficient out of the box, or perhaps may be subclassed to enhance one or two parts of the process. If its largely not sufficient, I'd like to patch it to make up for its deficiencies.


ACTIONFORMS

ActionForms help by containing and validating the paramters of a request before any action is taken by the associated Action class. This is the place to make sure that any incoming data (form fields, etc) exist and/or are in the correct/expected format. The ActionForm's validate method is responsible for populating a Errors object with any errors that it finds, and return the object back to the RequestProcessor's main process method. The RequestProcessor can then, if there are errors, send the user back to where he came from to fix any problems.


ACTIONS

Actions are the main point of processing the data for a request. If an ActionForm class has been configured and the incoming data did not produce an Error object, the Action's execute() method will be called with the ActionForm as one of its arguments, allowing it to further act upon the data and do whatever it is that is expected of this request.


REQUEST


CONFIG


ERRORS


EXAMPLE

# in httpd.conf <Perl> sections or your startup.pl ...

use TieRod;

# configure TieRod globally %TieRod::Config = ( templateconfig => TieRod::Config::Template->new( PLUGIN_BASE => 'TieRod::Plugin', INCLUDE_PATH => '/Users/josh/tt', ABSOLUTE => 1, ), processorclass => 'TieRod::RequestProcessor', contenttype => 'text/html', nocache => 1, );

# map paths to their actions and stuff %TieRod::ActionMap = ( '/example' => { type => 'MyApp::Action::Example', actionform => 'MyApp::ActionForm::Example', input => '/', forward => [TieRod::Config::Forward->new( name => 'Success', path => 'example/action_one.tt',), TieRod::Config::Forward->new( name => 'Failure', path => '/'),] }, );

# in httpd.conf <Location /myapp> SetHandler perl-script PerlHandler TieRod::ActionHandler PerlSendHeader Off </Location>

Then, create your Action and ActionForm classes ...

# MyApp::ActionForm::Example.pm

package MyApp::ActionForm::Example; use base qw(TieRod::ActionForm);

sub validate { my $self = shift; my %errors = ();

$errors{exists} = 1 unless $self->exists(); $errors{digit} = 1 unless $self->digit() && $self->digit =~ /^\d+$/; $errors{string} = 1 unless $self->string() && $self->string =~ /[A-Za-z]+/; return keys %errors ? \%errors : undef; }

# MyApp::Action::Example.pm

package MyApp::Action::Example; use base qw(TieRod::Action);

sub execute { my ($self, $mapping, $form, $r) = @_;

my $data = {string => $form->string, digit => $form->digit, exists => $form->exists, }; my $session = $r->session; $session->{data} = $data; my $forward = undef; foreach (@{$mapping->{forward}}) { $forward = $_ if $_->name() eq 'Success'; } return $forward; }


AUTHOR

Josh Rotenberg <josh@parasite.com>


VERSION

TieRod 0.01


SEE ALSO


COPYRIGHT AND LICENSE

Copyright (C) 2004 by Josh Rotenberg

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.