[Catalyst-dev] more intelligence inside $c->uri_for()

J. Shirley jshirley at gmail.com
Thu Apr 30 06:20:12 GMT 2009


On Thu, Apr 30, 2009 at 2:54 PM, Wolfgang Kinkeldei
<wolfgang at kinkeldei.de>wrote:

> Hi everyone,
>
> currently I am building a small JS/CSS-combining and minifying controller.
> To build my URI I am doing business as usual:
>
>  $c->uri_for(MyApp->Controller('Js')->action_for('default'), qw(list of
> js-files to combine));
>
> yielding (after some hacks) a URI like:
>
>  http://localhost/js/list/of/js-files/to/combine.js?m=3D12345667
>
> which works fine. However, I would like to automatically get added a
> Query-Parameter after the URI that reflects the timestamp of the most-rec=
ent
> file in the URI to allow the browser to do caching properly. As far as I
> understand there is currently no way to let the Controller construct the
> URI.
>
>

This really seems like a good controller base class, rather than something
"out of the box".  There is a Catalyst::View::JavaScript::Minifier, but it
seems just a thin layer on top of JavaScript::Minifier::XS and doesn't
handle any cache headers, etc.

I'm also torn on using the most recent mtime as a timestamp in the query
parameter... seems cleaner to have a version scheme for your resources to
bundle them together.

To answer your question, Chained lets you create any URL scheme you can
reasonably think of.

Assuming 'magic_method' gives you the valid files and the mtime, you
probably want something like this:

package MyApp::Controller::JS;

sub serve_files : Chained('/') PathPart('js') Args { ... }

sub generate_url : Private {
    my ( $self, $c, @files ) =3D @_;

    my @valid_files, $latest_mtime =3D magic_method(@files);

    $c->uri_for(
        # The $action object
        $c->controller('JS')->action_for('serve_files'),
        # Args to pass in (Yay Chained!)
        @valid_files,
        # Query parameters to append
        { mtime =3D $latest_mtime }
    );
}

1;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst-dev/attachments/20090430/c=
beb25e1/attachment.htm


More information about the Catalyst-dev mailing list