A handler is a subroutine tied to a specific URL.
Choose a suitable name space for your handler, we'll use Act::Handler::Foo
.
Create your handler in lib/Act/Handler/Foo.pm, naming your sub handler
.
handler
subroutineIn your handler
subroutine:
Figure out what to do, based on the query's arguments $Request{args}
or maybe using $Request{path_info}
.
Do it. You might need to query the database with $Request{dbh}
.
In most cases you'll want to send an HTML page using a template. First Retrieve a template object:
my $template = Act::Template::HTML->new;
Always instantiate this template object at runtime (Act uses its own caching mechanism to pull the correct template.)
Create some template variables:
$template->variables( name => $user->{name}, );
Finally process the template. Its output will be sent to the client's browser.
$template->process('foo.html');
Just provide the template's base name, don't use pathnames-- the template object knows about the current request and will know where to look. Template files in conference subdirectories override global templates.
Now choose an "action" word that best describes your handler. We'll use
the string foo
. We now associate that action word with our sub by
adding the mapping to the %public_handlers
hash in Act::Dispatcher
.
my %public_handlers = ( ... foo => 'Act::Handler::Foo', ... );
This will enable this handler for all URLs that start with /conference/foo where conference is a conference name, e.g.:
/2004/foo /2004/foo?q=1 /2004/foo/bar
If this action requires the user to be registered, add the mapping to
%private_handlers
instead:
my %private_handlers = ( ... foo => 'Act::Handler::Foo, ... );
The user will be prompted for credentials (login and password) the first time a protected page is requested in a given session.