NAME

Catalyst::Plugin::CachedUriForAction - drop-in supercharger for uri_for_action

SYNOPSIS

 use Catalyst qw( CachedUriForAction );

DESCRIPTION

This provides a (mostly) drop-in replacement version of uri_for_action.

The stock Catalyst uri_for_action method is a thin wrapper around uri_for. Every time you pass uri_for an action to create a parametrized URL for it, it introspects the dispatcher. This is expensive, and on views that generate a lot of URLs, it can add up to a substantial cost. Doing this introspection repeatedly can only possibly be useful if the set of controllers and actions in the application can change at runtime. Even then it is still wasted time on any view that generates many URLs for the same action.

This plugin scans the dispatch table once during startup and pregenerates templates for all possible output URLs. The only work then left in uri_for_action is the string manipulation to assemble a URL from its template.

LIMITATIONS

The following things are unsupported in this plugin:

  • Controller and action addition/removal at runtime

    This is by design and not likely to ever change.

    If you need this then you will not be able to use this plugin.

  • Incorrect uri_for_action inputs

    The stock method returns undef when given an unknown action path or the wrong number of captures or args. This has never been useful to me but has been a cause of some annoying debugging sessions. This plugin puts an end to that by throwing an exception instead.

    If you run into this, you can use eval or fall back to uri_for for those calls.

  • Setting the URL fragment as part of the args

    This plugin does not handle args in the sloppy/DWIM fashion uri_for tries to offer. Setting a URL fragment is supported, but only by passing it as a trailing scalar ref. Plain parameters are always treated as args and therefore encoded.

    If you run into this, you can fall back to uri_for for those calls.

  • Arg constraints (such as :CaptureArgs(Int,Str))

    Note that this plugin does not affect request dispatch so constraints will still apply there. They will merely not be validated when generating URLs.

    This may be possible to support but demand would have to justify an attempt at it.

  • "\0\0\0\0" in the PathPart of any action

    This string is internally used as a marker for placeholder values. The dispatch table scanner will generate bogus templates for such actions. This is mentioned here just for completeness as it seems unlikely to bite anyone in practice.

    If you do run into this, you can fall back to uri_for for those actions.

AUTHOR

Aristotle Pagaltzis <pagaltzis@gmx.de>

COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Aristotle Pagaltzis.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.