I am also implementing a MVC pattern under mod_perl for an internal project
at work.  The app collects process data from our manufacturing processes and
makes it available on the internal network.  Here's a quick overview of the
way I've done it.

Model:
  The model consists of a backend database (MySQL) and a set of perl classes
that interact with it.  Each perl class encapsulates the data and methods
associated with an entity in the application (like an Assembly, Company,
etc).  These classes know nothing of mod_perl.  The only other thing worth
mentioning is that each entity has a method called "to_hash" that creates a
hash representation of itself that can be plugged directly into a HTML
template (see View below).

View:
  The view consists of a set of templates developed using the Template
Toolkit.  I strongly believe that templates should only display data, not
generate it (no dbase calls).  The controller creates the dynamic data,
stuffs it into the template, and sends it back to the user.

Controller:
  The core controller (an Apache handler) is responsible for authorization,
gathering user input, performing any actions, and returning the appropriate
view to the user.    Whenever it performs an action, the controller needs to
create and monkey with a rather large set of Entity classes.  To simplify
the code in the controller, I developed a set of Facade classes that are
responsible for chunks of related actions.  So, the controller instantiates
a Facade and calls it's methods.  The results are plugged directly into a
template.

Only Entity classes speak SQL.  Only templates speak HTML.  Everything else
works with object methods or hashes.

Whenever I need an internal redirect, I use CGI encoded URLs, just as Dom
mentioned in his email.

I don't know if this is the best design, but it works for this application.
If you made it this far into the email, you might be interested in some
sample code...let me know.  If you have comments, please speak up.  I'm the
only developer on the project, so if I've gone off the deep end, I might not
notice.

Thanks,
Tim


On Tue, 26 Feb 2002, F. Xavier Noria wrote:

> As an exercise studying mod_perl I am trying to see how could the MVC
> pattern be implemented.  I've thought a possible approach would be to
> write the model using normal Perl classes, and controllers and views
> with Apache modules.
>
> I suppose that controllers would use internal redirects to call the
> views, is there a way to pass Perl data this way?  For example, in the
> hangman game in O'Reilly's book a controller would load a session from
> the cookie, process user's guest, modify the state and redirect the
> request internally to the view.  Ideally the view shouldn't read the
> data to display from the database again... could it be passed somehow by
> the first content handler

Reply via email to