MVC-style Programming

While PET can be used as some sort of an “active pages” system, that is clearly not the recommended usage.  What you will most likely be using is the MVC-style programming method of PET.

Summary
MVC-style ProgrammingWhile PET can be used as some sort of an “active pages” system, that is clearly not the recommended usage.
Actions and APIsMVC programming (MVC : Modell-View-Controller) separates the programming logic (what your application does) from the representation (what the users see).
The ActionMapper(Will be completed later...)

Actions and APIs

MVC programming (MVC : Modell-View-Controller) separates the programming logic (what your application does) from the representation (what the users see).  According to the classical MVC paradigm, the Model is your application (and databases), the View is the collection of .pet files, while the Controller is the PET framework itself (well... sort of -- see : http://en.wikipedia.org/wiki/MVC_Design_Pattern).

Let’s see a simple “Hello, World!” style example!

Example 1

”Hello, World!”  -- MVC style.

In your “index.pet”, you have

...
[% subst.msg %]
...

In your “MyApp.pm” you have

...
sub ACTION_index {
    my $self = shift;

    return {
        'msg'   =>  'Hello, World!'
    };
}
...

Discussion

PET can associate methods with files-s -- more precisely, it maps methods of modules to URL-s.  The way how this mapping is done can be configured -- you can even create your own mapper (we call them “ActionMapper-s”).

For example : http://www.mydomain.com/index.pet - can be mapped to MyApp::ACTION_index, using <ActionMapper::Single> http://www.mydomain.com/admin/login.pet - can be mapped to MyApp::admin::ACTION_index, using <ActionMapper::Subdir>

When a request reaches the PET server -- if it is configured so -- it looks up if a module/method is associated to that particular URL.  If there is a method, PET invokes the method, and puts its return value -- which must be a hashref -- into the variable “subst”, which is then passed to the correspondng .pet file.

If the method return a string instead of a hashref, PET redirects (HTTP 302) the users browser to that address.

Example 2

Logging in using sessions -- MVC style

(Will be completed later...)

The ActionMapper

(Will be completed later...)

Close