Catalyst-Plugin-URI
view release on metacpan or search on metacpan
README.mkdn view on Meta::CPAN
# METHODS
This plugin adds the following methods to your context
## uri
Example:
$c->uri("$controller:$action", \@parts, \%query, \$fragment);
This is a sugar method which works the same as:
my $url = $c->uri_for(
$c->controller($controller)->action_for($action),
\@args, \%query, \$fragment);
Just a bit shorter, and also we check to make sure the $controller and
$action actually exist (and raise a hard fail if they don't with an error
message that is I think more clear than the longer version.
You can also use a 'relative' specification for the action, which assumes
the current controller. For example:
$c->uri(":$action", \@parts, \%query, \$fragment);
Basically the same as:
my $url = $c->uri_for(
$self->action_for($action),
\@args, \%query, \$fragment);
We also support a corrected version of what 'uri\_for\_action' meant to achieve:
$c->uri("$action", @args);
Basically the same as:
my $url = $c->uri_for($self->action_for($action), @args);
Where the $action string is the full or relative (to the current controller) private
name of the action. Please note this does support path traversal with '..' so the
following means "create a URL to an action in the controller namespace above the
current one":
my $url = $c->uri("../foo"); # $c->uri($self->action_for("../foo"));
Experimentally we support named actions so that you can specify a link with a custom
name:
sub name_action :Local Args(0) Name(hi) {
my ($self, $c) = @_;
# rest of action
}
my $url = $c->uri("#hi");
This allows you to specify the action by its name from any controller. We don't
allow you to use the same name twice, and we also throw an exception if you attempt
to add a name to an intermediate action in a chain of actions (you can only name
an endpoint).
Lastly For ease of use if the first argument is an action object we just pass it
down to 'uri\_for'. That way you should be able to use this method for all types
of URL creation.
# OTHER SIMILAR OPTIONS
[Catalyst](https://metacpan.org/pod/Catalyst) offers a second way to make URLs that use the action private
name, the 'uri\_for\_action' method. However this suffers from a bug where
'path/action' and '/path/action' work the same (no support for relative
actions). Also this doesn't give you a very good error message if the action
private path does not exist, leading to difficult debugging issues sometimes.
Lastly I just personally prefer to look up an action via $controller->action\_for(...)
over the private path, which is somewhat dependent on controller namespace
information that you might change.
Prior art on CPAN doesn't seem to solve issues that I think actually exist (
for example older versions of [Catalyst](https://metacpan.org/pod/Catalyst) required that you specify capture
args from args in a Chained action, there's plugins to address that but that
was fixed in core [Catalyst](https://metacpan.org/pod/Catalyst) quite a while ago.) This plugin exists merely as
sugar over the formal syntax and tries to do nothing else fancy.
# AUTHOR
John Napiorkowski [email:jjnapiork@cpan.org](email:jjnapiork@cpan.org)
# SEE ALSO
[Catalyst](https://metacpan.org/pod/Catalyst)
# COPYRIGHT & LICENSE
Copyright 2015, John Napiorkowski [email:jjnapiork@cpan.org](email:jjnapiork@cpan.org)
This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
( run in 2.751 seconds using v1.01-cache-2.11-cpan-5837b0d9d2c )