[Catalyst] Re: Various ways to use c.uri_for

Aristotle Pagaltzis pagaltzis at gmx.de
Sat Jan 9 19:20:10 GMT 2010


* Octavian Rasnita <orasnita at gmail.com> [2010-01-09 15:20]:
> It could be helpful to have shortcuts for the second way of
> creating URLS, something like...
>
> c.url_query('controller_name', 'action_name', param1, param2)

Already exists, although it uses the internal path instead of
separately passing the controller package and the action name:

    c.uri_for_action('/controller/action', param1, param2)

Note that this is *not* a hardcoded URI! The above this is not
same as the following:

    c.uri_for('/controller/action', param1, param2)

When you use `uri_for_action`, you are referring to the action
paths listed in the debug info table that you see when you start
the Catalyst server in debug mode. If you use attributes to
change the URI an action matches, then the `uri_for_action`
output will also automatically reflect that change.

> We can put a url_query subroutine in MyApp.pm that looks like
> the one below (although I don't know if it works in all the
> cases - chains),

It works just fine with chains. It will follow the chain backward
and use any captures you supply to construct a URI that will
dispatch to the end point you specified. F.ex. I have the
following:

    package MyApp::Controller::Staff;
    sub base : Chained PathPart('app') CaptureArgs(0) { ... }
    package MyApp::Controller::Person;
    sub base : Chained('/staff/base') PathPart('person') CaptureArgs(0) { ... }
    sub list : Chained('base') PathPart('') Args(0) { ... }
    sub item : Chained('base') PathPart('') CaptureArgs(1) { ... }
    sub edit : Chained('item') Args(0) { ... }

Now if I say this:

    c.uri_for_action('/person/edit', [42])

that will give me a URI that looks like this:

    /app/person/42/edit

Here you can clearly see that the first argument refers to action
paths, not URIs. The URI is constructed out of the PathParts of
all the actions that participate in the chain.

> but it could be helpful to have such a thing in the core:

As you see, there already is. :-)

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>



More information about the Catalyst mailing list