.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{
. if \nF \{
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "CGI::Application::Dispatch 3pm"
.TH CGI::Application::Dispatch 3pm "2015-06-07" "perl v5.20.2" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
CGI::Application::Dispatch \- Dispatch requests to CGI::Application based objects
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.SS "Out of Box"
.IX Subsection "Out of Box"
Under mod_perl:
.PP
.Vb 4
\&
\& SetHandler perl\-script
\& PerlHandler CGI::Application::Dispatch
\&
.Ve
.PP
Under normal cgi:
.PP
This would be the instance script for your application, such
as /cgi\-bin/dispatch.cgi:
.PP
.Vb 5
\& #!/usr/bin/perl
\& use FindBin::Real \*(AqBin\*(Aq;
\& use lib Bin() . \*(Aq/../../rel/path/to/my/perllib\*(Aq;
\& use CGI::Application::Dispatch;
\& CGI::Application::Dispatch\->dispatch();
.Ve
.SS "With a dispatch table"
.IX Subsection "With a dispatch table"
.Vb 2
\& package MyApp::Dispatch;
\& use base \*(AqCGI::Application::Dispatch\*(Aq;
\&
\& sub dispatch_args {
\& return {
\& prefix => \*(AqMyApp\*(Aq,
\& table => [
\& \*(Aq\*(Aq => { app => \*(AqWelcome\*(Aq, rm => \*(Aqstart\*(Aq },
\& \*(Aq:app/:rm\*(Aq => { },
\& \*(Aqadmin/:app/:rm\*(Aq => { prefix => \*(AqMyApp::Admin\*(Aq },
\& ],
\& };
\& }
.Ve
.PP
Under mod_perl:
.PP
.Vb 4
\&
\& SetHandler perl\-script
\& PerlHandler MyApp::Dispatch
\&
.Ve
.PP
Under normal cgi:
.PP
This would be the instance script for your application, such
as /cgi\-bin/dispatch.cgi:
.PP
.Vb 5
\& #!/usr/bin/perl
\& use FindBin::Real \*(AqBin\*(Aq;
\& use lib Bin() . \*(Aq/../../rel/path/to/my/perllib\*(Aq;
\& use MyApp::Dispatch;
\& MyApp::Dispatch\->dispatch();
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
This module provides a way (as a mod_perl handler or running under
vanilla \s-1CGI\s0) to look at the path (as returned by dispatch_path) of
the incoming request, parse off the desired module and its run mode,
create an instance of that module and run it.
.PP
It currently supports both generations of mod_perl (1.x and
2.x). Although, for simplicity, all examples involving Apache
configuration and mod_perl code will be shown using mod_perl 1.x.
This may change as mp2 usage increases.
.PP
It will translate a \s-1URI\s0 like this (under mod_perl):
.PP
.Vb 1
\& /app/module_name/run_mode
.Ve
.PP
or this (vanilla cgi)
.PP
.Vb 1
\& /app/index.cgi/module_name/run_mode
.Ve
.PP
into something that will be functionally similar to this
.PP
.Vb 2
\& my $app = Module::Name\->new(..);
\& $app\->mode_param(sub {\*(Aqrun_mode\*(Aq}); #this will set the run mode
.Ve
.SH "METHODS"
.IX Header "METHODS"
.SS "dispatch(%args)"
.IX Subsection "dispatch(%args)"
This is the primary method used during dispatch. Even under mod_perl,
the handler method uses this under the hood.
.PP
.Vb 3
\& #!/usr/bin/perl
\& use strict;
\& use CGI::Application::Dispatch;
\&
\& CGI::Application::Dispatch\->dispatch(
\& prefix => \*(AqMyApp\*(Aq,
\& default => \*(Aqmodule_name\*(Aq,
\& );
.Ve
.PP
This method accepts the following name value pairs:
.IP "default" 4
.IX Item "default"
Specify a value to use for the path if one is not available.
This could be the case if the default page is selected (eg: \*(L"/\*(R" ).
.IP "prefix" 4
.IX Item "prefix"
This option will set the string that will be prepended to the name of
the application module before it is loaded and created. So to use our
previous example request of
.Sp
.Vb 1
\& /app/index.cgi/module_name/run_mode
.Ve
.Sp
This would by default load and create a module named
\&'Module::Name'. But let's say that you have all of your application
specific modules under the 'My' namespace. If you set this option to
\&'My' then it would instead load the 'My::Module::Name' application
module instead.
.IP "args_to_new" 4
.IX Item "args_to_new"
This is a hash of arguments that are passed into the \f(CW\*(C`new()\*(C'\fR
constructor of the application.
.IP "table" 4
.IX Item "table"
In most cases, simply using Dispatch with the \f(CW\*(C`default\*(C'\fR and \f(CW\*(C`prefix\*(C'\fR
is enough to simplify your application and your URLs, but there are
many cases where you want more power. Enter the dispatch table. Since
this table can be slightly complicated, a whole section exists on its
use. Please see the \*(L"\s-1DISPATCH TABLE\*(R"\s0 section.
.IP "debug" 4
.IX Item "debug"
Set to a true value to send debugging output for this module to
\&\s-1STDERR.\s0 Off by default.
.IP "error_document" 4
.IX Item "error_document"
This string is similar to Apache ErrorDocument directive. If this value is not
present, then Dispatch will return a \s-1NOT FOUND\s0 error either to the browser with
simple hardcoded message (under \s-1CGI\s0) or to Apache (under mod_perl).
.Sp
This value can be one of the following:
.Sp
\&\fBA string with error message\fR
\&\- if it starts with a single double-quote character (\f(CW\*(C`"\*(C'\fR). This double-quote
character will be trimmed from final output.
.Sp
\&\fBA file with content of error document\fR
\&\- if it starts with less-than sign (\f(CW\*(C`<\*(C'\fR). First character will be excluded
as well. Path of this file should be relative to server \s-1DOCUMENT_ROOT.\s0
.Sp
\&\fBA \s-1URI\s0 to which the application will be redirected\fR \- if no leading \f(CW\*(C`"\*(C'\fR or
\&\f(CW\*(C`<\*(C'\fR will be found.
.Sp
Custom messages will be displayed \fIin non mod_perl environment only\fR. (Under
mod_perl, please use ErrorDocument directive in Apache configuration files.)
This value can contain \f(CW%s\fR placeholder for sprintf Perl function. This
placeholder will be replaced with numeric \s-1HTTP\s0 error code. Currently
CGI::Application::Dispatch uses three \s-1HTTP\s0 errors:
.Sp
\&\fB400 Bad Request\fR
\&\- If there are invalid characters in module name (parameter :app) or
runmode name (parameter :rm).
.Sp
\&\fB404 Not Found\fR
\&\- When the path does not match anything in the \*(L"\s-1DISPATCH TABLE\*(R"\s0,
or module could not be found in \f(CW@INC\fR, or run mode did not exist.
.Sp
\&\fB500 Internal Server Error\fR
\&\- If application error occurs.
.Sp
Examples of using error_document (assume error 404 have been returned):
.Sp
.Vb 2
\& # return in browser \*(AqOpss... HTTP Error #404\*(Aq
\& error_document => \*(Aq"Opss... HTTP Error #%s\*(Aq
\&
\& # return contents of file $ENV{DOCUMENT_ROOT}/errors/error404.html
\& error_document => \*(Aq \*(Aq/errors/error%s.html\*(Aq
\&
\& # external redirect to
\& # http://host.domain/cgi\-bin/errors.cgi?error=404
\& error_document => \*(Aqhttp://host.domain/cgi\-bin/errors.cgi?error=%s\*(Aq
.Ve
.IP "auto_rest" 4
.IX Item "auto_rest"
This tells Dispatch that you are using \s-1REST\s0 by default and that you
care about which \s-1HTTP\s0 method is being used. Dispatch will append the
\&\s-1HTTP\s0 method name (upper case by default) to the run mode that is
determined after finding the appropriate dispatch rule. So a \s-1GET\s0
request that translates into \f(CW\*(C`MyApp::Module\->foo\*(C'\fR will become
\&\f(CW\*(C`MyApp::Module\->foo_GET\*(C'\fR.
.Sp
This can be overridden on a per-rule basis in a custom dispatch table.
.IP "auto_rest_lc" 4
.IX Item "auto_rest_lc"
In combinaion with auto_rest this tells Dispatch that you prefer
lower cased \s-1HTTP\s0 method names. So instead of \f(CW\*(C`foo_POST\*(C'\fR and
\&\f(CW\*(C`foo_GET\*(C'\fR you'll have \f(CW\*(C`foo_post\*(C'\fR and \f(CW\*(C`foo_get\*(C'\fR.
.SS "\fIdispatch_path()\fP"
.IX Subsection "dispatch_path()"
This method returns the path that is to be processed.
.PP
By default it returns the value of \f(CW$ENV{PATH_INFO}\fR
(or \f(CW\*(C`$r\->path_info\*(C'\fR under mod_perl) which should work for
most cases. It allows the ability for subclasses to override the value if
they need to do something more specific.
.SS "\fIhandler()\fP"
.IX Subsection "handler()"
This method is used so that this module can be run as a mod_perl handler.
When it creates the application module it passes the \f(CW$r\fR argument into the \s-1PARAMS\s0
hash of \fInew()\fR
.PP
.Vb 6
\&
\& SetHandler perl\-script
\& PerlHandler CGI::Application::Dispatch
\& PerlSetVar CGIAPP_DISPATCH_PREFIX MyApp
\& PerlSetVar CGIAPP_DISPATCH_DEFAULT /module_name
\&
.Ve
.PP
The above example would tell apache that any url beginning with /app
will be handled by CGI::Application::Dispatch. It also sets the prefix
used to create the application module to 'MyApp' and it tells
CGI::Application::Dispatch that it shouldn't set the run mode but that
it will be determined by the application module as usual (through the
query string). It also sets a default application module to be used if
there is no path. So, a url of \f(CW\*(C`/app/module_name\*(C'\fR would create an
instance of \f(CW\*(C`MyApp::Module::Name\*(C'\fR.
.PP
Using this method will add the \f(CW\*(C`Apache\-\*(C'\fRrequest> object to your
application's \f(CW\*(C`PARAMS\*(C'\fR as 'r'.
.PP
.Vb 2
\& # inside your app
\& my $request = $self\->param(\*(Aqr\*(Aq);
.Ve
.PP
If you need more customization than can be accomplished with just
prefix and default, then it would be best to just subclass
CGI::Application::Dispatch and override dispatch_args since
\&\f(CW\*(C`handler()\*(C'\fR uses dispatch to do the heavy lifting.
.PP
.Vb 2
\& package MyApp::Dispatch;
\& use base \*(AqCGI::Application::Dispatch\*(Aq;
\&
\& sub dispatch_args {
\& return {
\& prefix => \*(AqMyApp\*(Aq,
\& table => [
\& \*(Aq\*(Aq => { app => \*(AqWelcome\*(Aq, rm => \*(Aqstart\*(Aq },
\& \*(Aq:app/:rm\*(Aq => { },
\& \*(Aqadmin/:app/:rm\*(Aq => { prefix => \*(AqMyApp::Admin\*(Aq },
\& ],
\& args_to_new => {
\& PARAMS => {
\& foo => \*(Aqbar\*(Aq,
\& baz => \*(Aqbam\*(Aq,
\& },
\& }
\& };
\& }
\&
\& 1;
.Ve
.PP
And then in your httpd.conf
.PP
.Vb 4
\&
\& SetHandler perl\-script
\& PerlHandler MyApp::Dispatch
\&
.Ve
.SS "\fIdispatch_args()\fP"
.IX Subsection "dispatch_args()"
Returns a hashref of args that will be passed to dispatch(). It
will return the following structure by default.
.PP
.Vb 8
\& {
\& prefix => \*(Aq\*(Aq,
\& args_to_new => {},
\& table => [
\& \*(Aq:app\*(Aq => {},
\& \*(Aq:app/:rm\*(Aq => {},
\& ],
\& }
.Ve
.PP
This is the perfect place to override when creating a subclass to
provide a richer dispatch table.
.PP
When called, it receives 1 argument, which is a reference to the hash
of args passed into dispatch.
.SS "translate_module_name($input)"
.IX Subsection "translate_module_name($input)"
This method is used to control how the module name is translated from
the matching section of the path (see \*(L"Path Parsing\*(R").
The main
reason that this method exists is so that it can be overridden if it
doesn't do exactly what you want.
.PP
The following transformations are performed on the input:
.IP "The text is split on '_'s (underscores) and each word has its first letter capitalized. The words are then joined back together and each instance of an underscore is replaced by '::'." 4
.IX Item "The text is split on '_'s (underscores) and each word has its first letter capitalized. The words are then joined back together and each instance of an underscore is replaced by '::'."
.PD 0
.IP "The text is split on '\-'s (hyphens) and each word has its first letter capitalized. The words are then joined back together and each instance of a hyphen removed." 4
.IX Item "The text is split on '-'s (hyphens) and each word has its first letter capitalized. The words are then joined back together and each instance of a hyphen removed."
.PD
.PP
Here are some examples to make it even clearer:
.PP
.Vb 3
\& module_name => Module::Name
\& module\-name => ModuleName
\& admin_top\-scores => Admin::TopScores
.Ve
.SS "require_module($module_name)"
.IX Subsection "require_module($module_name)"
This class method is used internally by CGI::Application::Dispatch to
take a module name (supplied by get_module_name) and require it in
a secure fashion. It is provided as a public class method so that if
you override other functionality of this module, you can still safely
require user specified modules. If there are any problems requiring
the named module, then we will \f(CW\*(C`croak\*(C'\fR.
.PP
.Vb 1
\& CGI::Application::Dispatch\->require_module(\*(AqMyApp::Module::Name\*(Aq);
.Ve
.SH "DISPATCH TABLE"
.IX Header "DISPATCH TABLE"
Sometimes it's easiest to explain with an example, so here you go:
.PP
.Vb 10
\& CGI::Application::Dispatch\->dispatch(
\& prefix => \*(AqMyApp\*(Aq,
\& args_to_new => {
\& TMPL_PATH => \*(Aqmyapp/templates\*(Aq
\& },
\& table => [
\& \*(Aq\*(Aq => { app => \*(AqBlog\*(Aq, rm => \*(Aqrecent\*(Aq},
\& \*(Aqposts/:category\*(Aq => { app => \*(AqBlog\*(Aq, rm => \*(Aqposts\*(Aq },
\& \*(Aq:app/:rm/:id\*(Aq => { app => \*(AqBlog\*(Aq },
\& \*(Aqdate/:year/:month?/:day?\*(Aq => {
\& app => \*(AqBlog\*(Aq,
\& rm => \*(Aqby_date\*(Aq,
\& args_to_new => { TMPL_PATH => "events/" },
\& },
\& ]
\& );
.Ve
.PP
So first, this call to dispatch sets the prefix and passes a
\&\f(CW\*(C`TMPL_PATH\*(C'\fR into args_to_new. Next it sets the table.
.SS "\s-1VOCABULARY\s0"
.IX Subsection "VOCABULARY"
Just so we all understand what we're talking about....
.PP
A table is an array where the elements are gouped as pairs (similar to
a hash's key-value pairs, but as an array to preserve order). The
first element of each pair is called a \f(CW\*(C`rule\*(C'\fR. The second element in
the pair is called the rule's \f(CW\*(C`arg list\*(C'\fR. Inside a rule there are
slashes \f(CW\*(C`/\*(C'\fR. Anything set of characters between slashes is called a
\&\f(CW\*(C`token\*(C'\fR.
.SS "\s-1URL MATCHING\s0"
.IX Subsection "URL MATCHING"
When a \s-1URL\s0 comes in, Dispatch tries to match it against each rule in
the table in the order in which the rules are given. The first one to
match wins.
.PP
A rule consists of slashes and tokens. A token can one of the following types:
.IP "literal" 4
.IX Item "literal"
Any token which does not start with a colon (\f(CW\*(C`:\*(C'\fR) is taken to be a literal
string and must appear exactly as-is in the \s-1URL\s0 in order to match. In the rule
.Sp
.Vb 1
\& \*(Aqposts/:category\*(Aq
.Ve
.Sp
\&\f(CW\*(C`posts\*(C'\fR is a literal token.
.IP "variable" 4
.IX Item "variable"
Any token which begins with a colon (\f(CW\*(C`:\*(C'\fR) is a variable token. These
are simply wild-card place holders in the rule that will match
anything in the \s-1URL\s0 that isn't a slash. These variables can later be
referred to by using the \f(CW\*(C`$self\->param\*(C'\fR mechanism. In the rule
.Sp
.Vb 1
\& \*(Aqposts/:category\*(Aq
.Ve
.Sp
\&\f(CW\*(C`:category\*(C'\fR is a variable token. If the \s-1URL\s0 matched this rule, then
you could retrieve the value of that token from whithin your
application like so:
.Sp
.Vb 1
\& my $category = $self\->param(\*(Aqcategory\*(Aq);
.Ve
.Sp
There are some variable tokens which are special. These can be used to
further customize the dispatching.
.RS 4
.IP ":app" 4
.IX Item ":app"
This is the module name of the application. The value of this token
will be sent to the translate_module_name method and then prefixed
with the prefix if there is one.
.IP ":rm" 4
.IX Item ":rm"
This is the run mode of the application. The value of this token will be the
actual name of the run mode used. The run mode can be optional, as
noted below. Example:
.Sp
.Vb 1
\& /foo/:rm?
.Ve
.Sp
If no run mode is found, it will default to using the \f(CW\*(C`start_mode()\*(C'\fR, just like
invoking CGI::Application directly. Both of these URLs would end up dispatching
to the start mode associated with /foo:
.Sp
.Vb 2
\& /foo/
\& /foo
.Ve
.RE
.RS 4
.RE
.IP "optional-variable" 4
.IX Item "optional-variable"
Any token which begins with a colon (\f(CW\*(C`:\*(C'\fR) and ends with a question
mark (>) is considered optional. If the rest of the \s-1URL\s0 matches the
rest of the rule, then it doesn't matter whether it contains this
token or not. It's best to only include optional-variable tokens at
the end of your rule. In the rule
.Sp
.Vb 1
\& \*(Aqdate/:year/:month?/:day?\*(Aq
.Ve
.Sp
\&\f(CW\*(C`:month?\*(C'\fR and \f(CW\*(C`:day?\*(C'\fR are optional-variable tokens.
.Sp
Just like with variable tokens, optional-variable tokens' values
can also be retrieved by the application, if they existed in the \s-1URL.\s0
.Sp
.Vb 3
\& if( defined $self\->param(\*(Aqmonth\*(Aq) ) {
\& ...
\& }
.Ve
.IP "wildcard" 4
.IX Item "wildcard"
The wildcard token \*(L"*\*(R" allows for partial matches. The token \s-1MUST\s0
appear at the end of the rule.
.Sp
.Vb 1
\& \*(Aqposts/list/*\*(Aq
.Ve
.Sp
By default, the \f(CW\*(C`dispatch_url_remainder\*(C'\fR param is set to the
remainder of the \s-1URL\s0 matched by the *. The name of the param can be
changed by setting \*(L"*\*(R" argument in the \*(L"\s-1ARG LIST\*(R"\s0.
.Sp
.Vb 1
\& \*(Aqposts/list/*\*(Aq => { \*(Aq*\*(Aq => \*(Aqpost_list_filter\*(Aq }
.Ve
.IP "method" 4
.IX Item "method"
You can also dispatch based on \s-1HTTP\s0 method. This is similar to using
auto_rest but offers more fine grained control. You include the
method (case insensitive) at the end of the rule and enclose it in
square brackets.
.Sp
.Vb 3
\& \*(Aq:app/news[post]\*(Aq => { rm => \*(Aqadd_news\*(Aq },
\& \*(Aq:app/news[get]\*(Aq => { rm => \*(Aqnews\*(Aq },
\& \*(Aq:app/news[delete]\*(Aq => { rm => \*(Aqdelete_news\*(Aq },
.Ve
.PP
The main reason that we don't use regular expressions for dispatch
rules is that regular expressions provide no mechanism for named back
references, like variable tokens do.
.SS "\s-1ARG LIST\s0"
.IX Subsection "ARG LIST"
Each rule can have an accompanying arg-list. This arg list can contain
special arguments that override something set higher up in dispatch
for this particular \s-1URL,\s0 or just have additional args passed available
in \f(CW\*(C`$self\->param()\*(C'\fR
.PP
For instance, if you want to override prefix for a specific rule,
then you can do so.
.PP
.Vb 1
\& \*(Aqadmin/:app/:rm\*(Aq => { prefix => \*(AqMyApp::Admin\*(Aq },
.Ve
.SH "Path Parsing"
.IX Header "Path Parsing"
This section will describe how the application module and run mode are
determined from the path if no \*(L"\s-1DISPATCH TABLE\*(R"\s0 is present, and what
options you have to customize the process. The value for the path to
be parsed is retrieved from the dispatch_path method, which by
default uses the \f(CW\*(C`PATH_INFO\*(C'\fR environment variable.
.SS "Getting the module name"
.IX Subsection "Getting the module name"
To get the name of the application module the path is split on
backslahes (\f(CW\*(C`/\*(C'\fR). The second element of the returned list (the first
is empty) is used to create the application module. So if we have a
path of
.PP
.Vb 1
\& /module_name/mode1
.Ve
.PP
then the string 'module_name' is used. This is passed through the
translate_module_name method. Then if there is a \f(CW\*(C`prefix\*(C'\fR (and
there should always be a prefix) it is added to the beginning of
this new module name with a double colon \f(CW\*(C`::\*(C'\fR separating the two.
.PP
If you don't like the exact way that this is done, don't fret you do
have a couple of options. First, you can specify a \*(L"\s-1DISPATCH TABLE\*(R"\s0
which is much more powerful and flexible (in fact this default
behavior is actually implemented internally with a dispatch table).
Or if you want something a little simpler, you can simply subclass and
extend the translate_module_name method.
.SS "Getting the run mode"
.IX Subsection "Getting the run mode"
Just like the module name is retrieved from splitting the path on
slashes, so is the run mode. Only instead of using the second element
of the resulting list, we use the third as the run mode. So, using the
same example, if we have a path of
.PP
.Vb 1
\& /module_name/mode2
.Ve
.PP
Then the string 'mode2' is used as the run mode.
.SH "MISC NOTES"
.IX Header "MISC NOTES"
.IP "\(bu" 8
\&\s-1CGI\s0 query strings
.Sp
\&\s-1CGI\s0 query strings are unaffected by the use of \f(CW\*(C`PATH_INFO\*(C'\fR to obtain
the module name and run mode. This means that any other modules you
use to get access to you query argument (ie, \s-1CGI\s0,
Apache::Request) should not be affected. But, since the run mode
may be determined by CGI::Application::Dispatch having a query
argument named 'rm' will be ignored by your application module.
.SH "CLEAN URLS WITH MOD_REWRITE"
.IX Header "CLEAN URLS WITH MOD_REWRITE"
With a dispatch script, you can fairly clean \s-1URLS\s0 like this:
.PP
.Vb 1
\& /cgi\-bin/dispatch.cgi/module_name/run_mode
.Ve
.PP
However, including \*(L"/cgi\-bin/dispatch.cgi\*(R" in ever \s-1URL\s0 doesn't add any
value to the \s-1URL,\s0 so it's nice to remove it. This is easily done if
you are using the Apache web server with \f(CW\*(C`mod_rewrite\*(C'\fR
available. Adding the following to a \f(CW\*(C`.htaccess\*(C'\fR file would allow you
to simply use:
.PP
.Vb 1
\& /module_name/run_mode
.Ve
.PP
If you have problems with mod_rewrite, turn on debugging to see
exactly what's happening:
.PP
.Vb 2
\& RewriteLog /home/project/logs/alpha\-rewrite.log
\& RewriteLogLevel 9
.Ve
.SS "mod_rewrite related code in the dispatch script."
.IX Subsection "mod_rewrite related code in the dispatch script."
This seemed necessary to put in the dispatch script to make mod_rewrite happy.
Perhaps it's specific to using \f(CW\*(C`RewriteBase\*(C'\fR.
.PP
.Vb 3
\& # mod_rewrite alters the PATH_INFO by turning it into a file system path,
\& # so we repair it.
\& $ENV{PATH_INFO} =~ s/^$ENV{DOCUMENT_ROOT}// if defined $ENV{PATH_INFO};
.Ve
.SS "Simple Apache Example"
.IX Subsection "Simple Apache Example"
.Vb 1
\& RewriteEngine On
\&
\& # You may want to change the base if you are using the dispatcher within a
\& # specific directory.
\& RewriteBase /
\&
\& # If an actual file or directory is requested, serve directly
\& RewriteCond %{REQUEST_FILENAME} !\-f
\& RewriteCond %{REQUEST_FILENAME} !\-d
\&
\& # Otherwise, pass everything through to the dispatcher
\& RewriteRule ^(.*)$ /cgi\-bin/dispatch.cgi/$1 [L,QSA]
.Ve
.ie n .SS "More complex rewrite: dispatching ""/"" and multiple developers"
.el .SS "More complex rewrite: dispatching ``/'' and multiple developers"
.IX Subsection "More complex rewrite: dispatching / and multiple developers"
Here is a more complex example that dispatches \*(L"/\*(R", which would otherwise
be treated as a directory, and also supports multiple developer directories,
so \f(CW\*(C`/~mark\*(C'\fR has its own separate dispatching system beneath it.
.PP
Note that order matters here! The Location block for \*(L"/\*(R" needs to come
before the user blocks.
.PP
.Vb 3
\&
\& RewriteEngine On
\& RewriteBase /
\&
\& # Run "/" through the dispatcher
\& RewriteRule ^home/project/www/$ /cgi\-bin/dispatch.cgi [L,QSA]
\&
\& # Don\*(Aqt apply this rule to the users sub directories.
\& RewriteCond %{REQUEST_URI} !^/~.*$
\& # If an actual file or directory is requested, serve directly
\& RewriteCond %{REQUEST_FILENAME} !\-f
\& RewriteCond %{REQUEST_FILENAME} !\-d
\& # Otherwise, pass everything through to the dispatcher
\& RewriteRule ^(.*)$ /cgi\-bin/dispatch.cgi/$1 [L,QSA]
\&
\&
\&
\& RewriteEngine On
\& RewriteBase /~mark
\&
\& # Run "/" through the dispatcher
\& RewriteRule ^/home/mark/www/$ /~mark/cgi\-bin/dispatch.cgi [L,QSA]
\&
\& # Otherwise, if an actual file or directory is requested,
\& # serve directly
\& RewriteCond %{REQUEST_FILENAME} !\-f
\& RewriteCond %{REQUEST_FILENAME} !\-d
\&
\& # Otherwise, pass everything through to the dispatcher
\& RewriteRule ^(.*)$ /~mark/cgi\-bin/dispatch.cgi/$1 [L,QSA]
\&
\& # These examples may also be helpful, but are unrelated to dispatching.
\& SetEnv DEVMODE mark
\& SetEnv PERL5LIB /home/mark/perllib:/home/mark/config
\& ErrorDocument 404 /~mark/errdocs/404.html
\& ErrorDocument 500 /~mark/errdocs/500.html
\&
.Ve
.SH "SUBCLASSING"
.IX Header "SUBCLASSING"
While Dispatch tries to be flexible, it won't be able to do everything
that people want. Hopefully we've made it flexible enough so that if
it doesn't do \fIThe Right Thing\fR you can easily subclass it.
.SH "AUTHOR"
.IX Header "AUTHOR"
Michael Peters
.PP
Thanks to Plus Three, \s-1LP \s0(http://www.plusthree.com) for sponsoring my
work on this module
.SH "COMMUNITY"
.IX Header "COMMUNITY"
This module is a part of the larger CGI::Application community. If
you have questions or comments about this module then please join us
on the cgiapp mailing list by sending a blank message to
\&\*(L"cgiapp\-subscribe@lists.erlbaum.net\*(R". There is also a community wiki
located at
.SH "SOURCE CODE REPOSITORY"
.IX Header "SOURCE CODE REPOSITORY"
A public source code repository for this project is hosted here:
.PP
http://code.google.com/p/cgi\-app\-modules/source/checkout
.SH "CONTRIBUTORS"
.IX Header "CONTRIBUTORS"
.IP "\(bu" 4
Shawn Sorichetti
.IP "\(bu" 4
Timothy Appnel
.IP "\(bu" 4
dsteinbrunner
.IP "\(bu" 4
\&\s-1ZACKSE\s0
.IP "\(bu" 4
Stew Heckenberg
.IP "\(bu" 4
Drew Taylor
.IP "\(bu" 4
James Freeman
.IP "\(bu" 4
Michael Graham
.IP "\(bu" 4
Cees Hek
.IP "\(bu" 4
Mark Stosberg
.IP "\(bu" 4
Viacheslav Sheveliov
.SH "SECURITY"
.IX Header "SECURITY"
Since C::A::Dispatch will dynamically choose which modules to use as
the content generators, it may give someone the ability to execute
random modules on your system if those modules can be found in you
path. Of course those modules would have to behave like
CGI::Application based modules, but that still opens up the door
more than most want. This should only be a problem if you don't use a
prefix. By using this option you are only allowing Dispatch to pick
from a namespace of modules to run.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
CGI::Application, Apache::Dispatch
.SH "COPYRIGHT & LICENSE"
.IX Header "COPYRIGHT & LICENSE"
Copyright Michael Peters and Mark Stosberg 2008, all rights reserved.
.PP
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.