| 1 |
NAME |
| 2 |
CGI::Application::Plugin::ActionDispatch - Perl extension |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
# In "WebApp.pm"... |
| 6 |
package WebApp; |
| 7 |
|
| 8 |
use base 'CGI::Application'; |
| 9 |
use CGI::Application::Plugin::ActionDispatch; |
| 10 |
|
| 11 |
sub do_stuff : Path('do/stuff') { ... } |
| 12 |
sub do_more_stuff : Regex('^/do/more/stuff\/?$') { ... } |
| 13 |
sub do_something_else : Regex('do/something/else/(\w+)/(\d+)$') { ... } |
| 14 |
|
| 15 |
DESCRIPTION |
| 16 |
CGI::Application::Plugin::ActionDispatch adds attribute based support |
| 17 |
for parsing the PATH_INFO of the incoming request. For those who are |
| 18 |
familiar with Catalyst. The interface works very similar. |
| 19 |
|
| 20 |
This plugin is plug and play and shouldn't interrupt the default |
| 21 |
behavior of CGI::Application. |
| 22 |
|
| 23 |
CAVEATS |
| 24 |
Be aware though, this plugin will not likely work with other modules |
| 25 |
that use attributes. |
| 26 |
|
| 27 |
This module should work with mod_perl. It however has not be thoroughly |
| 28 |
tested as such. If you have used it with mod_perl please e-mail me with |
| 29 |
your experience. |
| 30 |
|
| 31 |
METHODS |
| 32 |
action_args() |
| 33 |
If using capturing parentheses in a Regex action. The captured |
| 34 |
values are accessible using this method. |
| 35 |
|
| 36 |
sub addElement : Regex('add/(\d+)/(\d+)') { |
| 37 |
my $self = shift; |
| 38 |
my($column, $row) = $self->action_args(); |
| 39 |
... |
| 40 |
} |
| 41 |
|
| 42 |
The Path action will store everything after the matched path into |
| 43 |
the action args. |
| 44 |
|
| 45 |
# http://example.com/state/pa/philadelphia |
| 46 |
sub find_state_and_city : Path('state/') { |
| 47 |
my $self = shift; |
| 48 |
my($state, $city) = $self->action_args(); |
| 49 |
# $state == pa, $city == philadelphia |
| 50 |
... |
| 51 |
} |
| 52 |
|
| 53 |
ACTIONS |
| 54 |
Regex |
| 55 |
Regex action is used for regular expression matching against |
| 56 |
PATH_INFO. If capturing parentheses are used; the matched parameters |
| 57 |
are accesssible using the action_args() method. |
| 58 |
|
| 59 |
Regex('^blah/foo'); |
| 60 |
|
| 61 |
The Regex action either matches or it doesn't. There are no secrets |
| 62 |
to it. |
| 63 |
|
| 64 |
It is important to note Regex action takes priority. It is assumed |
| 65 |
if a Path and Regex action both match. The Regex action will take |
| 66 |
priority, which may not always be the outcome of least suprise, for |
| 67 |
instance: |
| 68 |
|
| 69 |
# http://example.com/music/the_clash sub clash : |
| 70 |
Path('/music/the_clash') {} # This is an exact match, BUT. sub |
| 71 |
the_class : Regex('/music/the_clash') {} # This takes priority. |
| 72 |
Beware. |
| 73 |
|
| 74 |
Path |
| 75 |
The Path action is basically a shortcut for a commonly used Regex |
| 76 |
action. |
| 77 |
|
| 78 |
# http://example.com/products/movies/2 |
| 79 |
sub show_product : Path('products/') { |
| 80 |
my $self = shift; |
| 81 |
my($category, $id) = $self->action_args(); |
| 82 |
.... |
| 83 |
} |
| 84 |
|
| 85 |
Is basically the same thing as. |
| 86 |
|
| 87 |
sub show_product : Regex('^/products/(\w+)/(\d+)') { |
| 88 |
my $self = shift; |
| 89 |
my($category, $id) = $self->action_args(); |
| 90 |
... |
| 91 |
} |
| 92 |
|
| 93 |
For those that care, the Path('products/') will be converted to the |
| 94 |
regular expression "^/products\/?(\/.*)$"; then split('/') is run on |
| 95 |
the captured value and stored in action_args(). |
| 96 |
|
| 97 |
Runmode |
| 98 |
This action will take the method name and run a match on that. |
| 99 |
|
| 100 |
# http://example.com/foobar |
| 101 |
|
| 102 |
sub foobar : Runmode {} |
| 103 |
|
| 104 |
Default |
| 105 |
The default run mode if no match is found. Essentially the |
| 106 |
equivalent of the start_mode() method. |
| 107 |
|
| 108 |
sub default_mode : Default {} |
| 109 |
|
| 110 |
EXAMPLE |
| 111 |
In CGI::Application module: |
| 112 |
|
| 113 |
package WebApp; |
| 114 |
|
| 115 |
use base 'CGI::Application'; |
| 116 |
use CGI::Application::Plugin::ActionDispatch; |
| 117 |
use strict; |
| 118 |
|
| 119 |
sub setup { |
| 120 |
my $self = shift; |
| 121 |
self->mode_param('test_rm'); |
| 122 |
$self->run_modes( |
| 123 |
basic_runmode => 'basic_runmode' |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
# Regular runmodes should work. |
| 128 |
sub basic_runmode { |
| 129 |
my $self = shift |
| 130 |
} |
| 131 |
|
| 132 |
The product() runmode will match anything starting with "/products" in |
| 133 |
the PATH_INFO. |
| 134 |
|
| 135 |
# http://example.com/myapp.cgi/products/this/is/optional/and/stored/in/action_args/ |
| 136 |
sub product : Path('products/') { |
| 137 |
my $self = shift; |
| 138 |
my($category, $product) = $self->action_args(); |
| 139 |
} |
| 140 |
|
| 141 |
The music() runmode will match anything starting with "/products/music" |
| 142 |
in the PATH_INFO. The product() runmode also matches "/products/music". |
| 143 |
However since this runmode matches closer it takes priority over |
| 144 |
product(). |
| 145 |
|
| 146 |
# http://example.com/myapp.cgi/products/music/product/ |
| 147 |
sub music : Path('products/music/') { |
| 148 |
my $self = shift; |
| 149 |
my $product = $self->action_args(); |
| 150 |
... |
| 151 |
} |
| 152 |
|
| 153 |
This beatles() runmode will match ONLY "/product/music/beatles" or |
| 154 |
"/product/music/beatles/". Regex takes priority over Path so the |
| 155 |
previous runmodes which match this PATH_INFO are not run. |
| 156 |
|
| 157 |
# http://example.com/myapp.cgi/products/music/beatles/ |
| 158 |
sub beatles : Regex('^/products/music/beatles\/?') { |
| 159 |
my $self = shift; |
| 160 |
... |
| 161 |
} |
| 162 |
|
| 163 |
SEE ALSO |
| 164 |
CGI::Application, CGI::Application::Dispatch |
| 165 |
|
| 166 |
AUTHOR |
| 167 |
Jason Yates, <[email protected]> |
| 168 |
|
| 169 |
COPYRIGHT AND LICENSE |
| 170 |
Copyright (C) 2006-2008 by Jason Yates |
| 171 |
|
| 172 |
This library is free software; you can redistribute it and/or modify it |
| 173 |
under the same terms as Perl itself, either Perl version 5.8.7 or, at |
| 174 |
your option, any later version of Perl 5 you may have available. |
| 175 |
|