| 1 | package Catalyst::Plugin::URI; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Moo::_Utils (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | requires 'uri_for'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = '0.006'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | my $uri_v1 = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 13 | 0 | my ($c, $path, @args) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # already is an $action | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | if(Scalar::Util::blessed($path) && $path->isa('Catalyst::Action')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($path) and $path->isa("Catalyst::Action")
MC/DC0%0/2
builtin::blessed($path) and $path->isa("Catalyst::Action")
builtin::blessed($path)
$path->isa("Catalyst::Action")
Truth table0%0/3
A and B
A= builtin::blessed($path)B= $path->isa("Catalyst::Action")
| |||||||||||||||||||||||||||||
| 17 | 0 | return $c->uri_for($path, @args); | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Hard error if the spec looks wrong... | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | croak "$path is not a string" unless ref \$path eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | croak "$path is not a controller.action specification" unless $path=~m/^(.*)\.(.+)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | croak "$1 is not a controller" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$1 || ''
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
| |||||||||||||||||||||||||||||
| 25 | unless my $controller = $c->controller($1||''); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | croak "$2 is not an action for controller ${\$controller->component_name}" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | unless my $action = $controller->action_for($2); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | return $c->uri_for($action, @args); | |||||||||||||||||||||||||||
| 31 | 2 | }; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 6 | ▶ | my $uri_v2 = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=16 SCAR=40.0
| |||||||||||||||||||||||||||||
| 34 | 6 | my ($c, $action_proto, @args) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # already is an $action | ||||||||||||||||||||||||||||
| 37 | 6 | ▶ | if(Scalar::Util::blessed($action_proto) && $action_proto->isa('Catalyst::Action')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($action_proto) and $action_proto->isa("Catalyst::Action")
MC/DC0%0/2
builtin::blessed($action_proto) and $action_proto->isa("Catalyst::Action")
builtin::blessed($action_proto)
$action_proto->isa("Catalyst::Action")
Truth table33%1/3
A and B
A= builtin::blessed($action_proto)B= $action_proto->isa("Catalyst::Action")
| |||||||||||||||||||||||||||||
| 38 | 0 | return $c->uri_for($action_proto, @args); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Hard error if the spec looks wrong... | ||||||||||||||||||||||||||||
| 42 | 6 | ▶ | croak "$action_proto is not a string" unless ref \$action_proto eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 6 | my $action; | |||||||||||||||||||||||||||
| 45 | 6 | ▶ | if($action_proto =~/^\/?#/) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | croak "$action_proto is not a named action" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | unless $action = $c->dispatcher->get_action_by_path($action_proto); | ||||||||||||||||||||||||||||
| 48 | } elsif($action_proto=~m/^(.*)\:(.+)$/) { | ||||||||||||||||||||||||||||
| 49 | 5 | ▶ | croak "$1 is not a controller" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$1 || ''
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||||||||||||
| 50 | unless my $controller = $c->controller($1||''); | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | croak "$2 is not an action for controller ${\$controller->component_name}" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | unless $action = $controller->action_for($2); | ||||||||||||||||||||||||||||
| 53 | } elsif($action_proto =~/\//) { | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $path = $action_proto=~m/^\// ? $action_proto : $c->controller->action_for($action_proto)->private_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | croak "$action_proto is not a full or relative private action path" unless $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | croak "$path is not a private path" unless $action = $c->dispatcher->get_action_by_path($path); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } elsif($action = $c->controller->action_for($action_proto)) { | ||||||||||||||||||||||||||||
| 58 | # Noop | ||||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||
| 60 | # Fallback to static | ||||||||||||||||||||||||||||
| 61 | 0 | $action = $action_proto; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 6 | ▶ | croak "We can't create a URI from $action with the given arguments" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | unless my $uri = $c->uri_for($action, @args); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 6 | return $uri; | |||||||||||||||||||||||||||
| 67 | 2 | }; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 4 | ▶ | my $uri_v3 = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=17 SCAR=44.5
| |||||||||||||||||||||||||||||
| 70 | 4 | my ($c, $action_proto, @args) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # already is an $action | ||||||||||||||||||||||||||||
| 73 | 4 | ▶ | if(Scalar::Util::blessed($action_proto) && $action_proto->isa('Catalyst::Action')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($action_proto) and $action_proto->isa("Catalyst::Action")
MC/DC0%0/2
builtin::blessed($action_proto) and $action_proto->isa("Catalyst::Action")
builtin::blessed($action_proto)
$action_proto->isa("Catalyst::Action")
Truth table33%1/3
A and B
A= builtin::blessed($action_proto)B= $action_proto->isa("Catalyst::Action")
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | die "We can't create a URI from '$action_proto' with the given arguments" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | unless my $uri = $c->uri_for($action_proto, @args); | ||||||||||||||||||||||||||||
| 76 | 0 | return $uri; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Hard error if the spec looks wrong... | ||||||||||||||||||||||||||||
| 80 | 4 | ▶ | die "$action_proto is not a string" unless ref \$action_proto eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 4 | my $action; | |||||||||||||||||||||||||||
| 83 | 4 | ▶ | if($action_proto =~/^\/?\*/) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | croak "$action_proto is not a named action" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | unless $action = $c->dispatcher->get_action_by_path($action_proto); | ||||||||||||||||||||||||||||
| 86 | } elsif($action_proto=~m/^(.*)\:(.+)$/) { | ||||||||||||||||||||||||||||
| 87 | 4 | ▶ | croak "$1 is not a controller" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$1 || ''
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||||||||||||
| 88 | unless my $controller = $c->controller($1||''); | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | croak "$2 is not an action for controller ${\$controller->component_name}" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | unless $action = $controller->action_for($2); | ||||||||||||||||||||||||||||
| 91 | } elsif($action_proto =~/\//) { | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my $path = $action_proto=~m/^\// ? $action_proto : $c->controller->action_for($action_proto)->private_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | croak "$action_proto is not a full or relative private action path" unless $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | croak "$path is not a private path" unless $action = $c->dispatcher->get_action_by_path($path); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } elsif($action = $c->controller->action_for($action_proto)) { | ||||||||||||||||||||||||||||
| 96 | # Noop | ||||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||
| 98 | # Fallback to static | ||||||||||||||||||||||||||||
| 99 | 0 | $action = $action_proto; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | ▶ | croak "We can't create a URI from $action with the given arguments" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | unless my $uri = $c->uri_for($action, @args); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 4 | return $uri; | |||||||||||||||||||||||||||
| 106 | 2 | }; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | after 'setup_finalize', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=15 SCAR=28.7
| |||||||||||||||||||||||||||||
| 110 | 2 | my ($c) = @_; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | my $version = 3; | |||||||||||||||||||||||||||
| 113 | 2 | ▶ | if (my $config = $c->config->{'Plugin::URI'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 2 | ▶ | $version = $config->{version} if exists $config->{version}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 2 | ▶ | $version = 1 if exists $config->{use_v1} and $config->{use_v1}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $config->{"use_v1"} and $config->{"use_v1"}
MC/DC0%0/2
exists $config->{"use_v1"} and $config->{"use_v1"}
exists $config->{"use_v1"}
$config->{"use_v1"}
Truth table33%1/3
A and B
A= exists $config->{"use_v1"}B= $config->{"use_v1"}
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 2 | ▶ | if($version == 1) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 119 | 0 | Moo::_Utils::_install_tracked($c, 'uri', $uri_v1); | |||||||||||||||||||||||||||
| 120 | } elsif($version == 2) { | ||||||||||||||||||||||||||||
| 121 | 1 | Moo::_Utils::_install_tracked($c, 'uri', $uri_v2); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # V2 does its own version of named actions | ||||||||||||||||||||||||||||
| 124 | 1 | ▶ | my %action_hash = %{$c->dispatcher->_action_hash||+{}}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$c->dispatcher->_action_hash || {}
MC/DC0%0/1
$c->dispatcher->_action_hash || {}
$c->dispatcher->_action_hash
Truth table50%1/2
A
A= $c->dispatcher->_action_hash
| |||||||||||||||||||||||||||||
| 125 | 1 | foreach my $key (keys %action_hash) { | |||||||||||||||||||||||||||
| 126 | 21 | ▶ | if(my ($name) = @{$action_hash{$key}->attributes->{Name}||[]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$action_hash{$key}->attributes->{'Name'} || []
MC/DC100%1/1
$action_hash{$key}->attributes->{'Name'} || []
$action_hash{$key}->attributes->{'Name'}
Truth table100%2/2
A
A= $action_hash{$key}->attributes->{'Name'}
| |||||||||||||||||||||||||||||
| 127 | croak "You can only name endpoint actions on a chain" | ||||||||||||||||||||||||||||
| 128 | 1 | ▶ | if defined$action_hash{$key}->attributes->{CaptureArgs}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | croak "Named action '$name' is already defined" | ||||||||||||||||||||||||||||
| 130 | 1 | ▶ | if $c->dispatcher->_action_hash->{"/#$name"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 1 | $c->dispatcher->_action_hash->{"/#$name"} = $action_hash{$key}; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 1 | foreach my $method(qw/detach forward visit go/) { | |||||||||||||||||||||||||||
| 135 | 31 | ▶ | Moo::_Utils::_install_modifier($c, 'around', $method, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 136 | 31 | my ($orig, $c, $action_proto, @args) = @_; | |||||||||||||||||||||||||||
| 137 | 31 | my $action; | |||||||||||||||||||||||||||
| 138 | 31 | ▶ | if(defined($action_proto) && $action_proto =~/^\/?#/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $action_proto and $action_proto =~ m[^/?#]
MC/DC50%1/2
defined $action_proto and $action_proto =~ m[^/?#]
defined $action_proto
$action_proto =~ m[^/?#]
Truth table66%2/3
A and B
A= defined $action_protoB= $action_proto =~ m[^/?#]
| |||||||||||||||||||||||||||||
| 139 | 1 | ▶ | die "$action_proto is not a named action" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | unless $action = $c->dispatcher->get_action_by_path($action_proto); | ||||||||||||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||||||||||
| 142 | 30 | $action = $action_proto; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 31 | $c->$orig($action, @args); | |||||||||||||||||||||||||||
| 145 | 4 | }); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | } elsif($version == 3) { | ||||||||||||||||||||||||||||
| 148 | 1 | Moo::_Utils::_install_tracked($c, 'uri', $uri_v3); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 2 | }; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 2 | 1; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head1 NAME | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | Catalyst::Plugin::URI - Yet another sugar plugin for $c->uri_for | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Use the plugin in your application class: | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | package MyApp; | ||||||||||||||||||||||||||||
| 163 | use Catalyst 'URI'; | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | MyApp->setup; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Then you can use it in your controllers: | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | package MyApp::Controller::Example; | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | use base 'Catalyst::Controller'; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub make_a_url :Local { | ||||||||||||||||||||||||||||
| 174 | my ($self, $c) = @_; | ||||||||||||||||||||||||||||
| 175 | my $url = $c->uri("$controller.$action", \@args, \%query, \$fragment); | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | This is just a shortcut with stronger error messages for: | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | sub make_a_url :Local { | ||||||||||||||||||||||||||||
| 181 | my ($self, $c) = @_; | ||||||||||||||||||||||||||||
| 182 | my $url = $c->url_for( | ||||||||||||||||||||||||||||
| 183 | $c->controller($controller)->action_for($action), | ||||||||||||||||||||||||||||
| 184 | \@args, \%query, \$fragment); | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | B<NOTE> Starting with version C<0.003> I changed that way this works. If you want | ||||||||||||||||||||||||||||
| 190 | or need the old API for backcompatibility please set the following configuration | ||||||||||||||||||||||||||||
| 191 | flag: | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | MyApp->config('Plugin::URI' => { version => 1 }); | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | B<NOTE> Starting with version C<0.005> we removed support for the hack that gave | ||||||||||||||||||||||||||||
| 196 | partial support for named actions. We are proposing this for L<Catalyst> core and | ||||||||||||||||||||||||||||
| 197 | this version in earlier versions of this code didn't properly work anyway (didn't | ||||||||||||||||||||||||||||
| 198 | support using an action name in a Chained declaration for example). If you are using | ||||||||||||||||||||||||||||
| 199 | and older Catalyst without core support for named actions and need the old behavior | ||||||||||||||||||||||||||||
| 200 | you can set it like this: | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | MyApp->config('Plugin::URI' => { version => 1 }); | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Currently if you want to create a URL to a controller's action properly the formal | ||||||||||||||||||||||||||||
| 205 | syntax is rather verbose: | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | my $url = $c->uri( | ||||||||||||||||||||||||||||
| 208 | $c->controller($controller)->action_for($action), | ||||||||||||||||||||||||||||
| 209 | \@args, \%query, \$fragment); | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Which is verbose enough that it probably encourages people to do the wrong thing | ||||||||||||||||||||||||||||
| 213 | and use a hard coded link path. This might later bite you if you need to change | ||||||||||||||||||||||||||||
| 214 | your controllers and URL hierarchy. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | Also, this can lead to weird error messages that don't make if clear that your | ||||||||||||||||||||||||||||
| 217 | $controller and $action are actually wrong. This plugin is an attempt to both | ||||||||||||||||||||||||||||
| 218 | make the proper formal syntax a bit more tidy and to deliver harder error messages | ||||||||||||||||||||||||||||
| 219 | if you get the names wrong. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =head1 METHODS | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | This plugin adds the following methods to your context | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head2 uri | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Example: | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | $c->uri("$controller:$action", \@parts, \%query, \$fragment); | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | This is a sugar method which works the same as: | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | my $url = $c->uri_for( | ||||||||||||||||||||||||||||
| 234 | $c->controller($controller)->action_for($action), | ||||||||||||||||||||||||||||
| 235 | \@args, \%query, \$fragment); | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | Just a bit shorter, and also we check to make sure the $controller and | ||||||||||||||||||||||||||||
| 238 | $action actually exist (and raise a hard fail if they don't with an error | ||||||||||||||||||||||||||||
| 239 | message that is I think more clear than the longer version. | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | You can also use a 'relative' specification for the action, which assumes | ||||||||||||||||||||||||||||
| 242 | the current controller. For example: | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | $c->uri(":$action", \@parts, \%query, \$fragment); | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Basically the same as: | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | my $url = $c->uri_for( | ||||||||||||||||||||||||||||
| 249 | $self->action_for($action), | ||||||||||||||||||||||||||||
| 250 | \@args, \%query, \$fragment); | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | We also support a corrected version of what 'uri_for_action' meant to achieve: | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | $c->uri("$action", @args); | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | Basically the same as: | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | my $url = $c->uri_for($self->action_for($action), @args); | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | Where the $action string is the full or relative (to the current controller) private | ||||||||||||||||||||||||||||
| 261 | name of the action. Please note this does support path traversal with '..' so the | ||||||||||||||||||||||||||||
| 262 | following means "create a URL to an action in the controller namespace above the | ||||||||||||||||||||||||||||
| 263 | current one": | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | my $url = $c->uri("../foo"); # $c->uri($self->action_for("../foo")); | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | Experimentally (in versions prior top C<0.005>) we support named actions so that you can specify a link with a custom | ||||||||||||||||||||||||||||
| 268 | name: | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | sub name_action :Local Args(0) Name(hi) { | ||||||||||||||||||||||||||||
| 271 | my ($self, $c) = @_; | ||||||||||||||||||||||||||||
| 272 | # rest of action | ||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | my $url = $c->uri("#hi"); | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | This allows you to specify the action by its name from any controller. We don't | ||||||||||||||||||||||||||||
| 278 | allow you to use the same name twice, and we also throw an exception if you attempt | ||||||||||||||||||||||||||||
| 279 | to add a name to an intermediate action in a chain of actions (you can only name | ||||||||||||||||||||||||||||
| 280 | an endpoint). | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | Lastly For ease of use if the first argument is an action object we just pass it | ||||||||||||||||||||||||||||
| 284 | down to 'uri_for'. That way you should be able to use this method for all types | ||||||||||||||||||||||||||||
| 285 | of URL creation. | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =head1 OTHER SIMILAR OPTIONS | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | L<Catalyst> offers a second way to make URLs that use the action private | ||||||||||||||||||||||||||||
| 290 | name, the 'uri_for_action' method. However this suffers from a bug where | ||||||||||||||||||||||||||||
| 291 | 'path/action' and '/path/action' work the same (no support for relative | ||||||||||||||||||||||||||||
| 292 | actions). Also this doesn't give you a very good error message if the action | ||||||||||||||||||||||||||||
| 293 | private path does not exist, leading to difficult debugging issues sometimes. | ||||||||||||||||||||||||||||
| 294 | Lastly I just personally prefer to look up an action via $controller->action_for(...) | ||||||||||||||||||||||||||||
| 295 | over the private path, which is somewhat dependent on controller namespace | ||||||||||||||||||||||||||||
| 296 | information that you might change. | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | Prior art on CPAN doesn't seem to solve issues that I think actually exist ( | ||||||||||||||||||||||||||||
| 299 | for example older versions of L<Catalyst> required that you specify capture | ||||||||||||||||||||||||||||
| 300 | args from args in a Chained action, there's plugins to address that but that | ||||||||||||||||||||||||||||
| 301 | was fixed in core L<Catalyst> quite a while ago.) This plugin exists merely as | ||||||||||||||||||||||||||||
| 302 | sugar over the formal syntax and tries to do nothing else fancy. | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | L<Catalyst> | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | Copyright 2023, John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 317 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =cut | ||||||||||||||||||||||||||||