[Catalyst] Relative links

Bill Moseley moseley at hank.org
Wed Nov 9 22:02:08 CET 2005


Relative links are biting me again, so I'm wondering how others
generate links in a sane way.

I have an action /admim/things that displays a table of rows.  I have
some common template code for displaying tables that include column
headings that can be clicked on for sorting by that column.  For those
headings I use relative links so the template code is portable:

    <th><a href="?o=date">Date</a></th>
    <th><a href="?o=name">Name</a></th>

That works great, in general.  Plus, it allows any additional
arguments to be included in the links.  (e.g. /admin/things/blue might
limit to just the blue things, so the sort would be
/admin/things/blue?o=date).

But that same feature causes trouble when forwarding from another
action.

An example might be where I re-show the list after deleting an item:

    package App::C::Admin::Thing;

    sub delete {
        my ($self, $c, $id ) = @_;
        DB::Thing->retrieve($id)->delete;
        $c->stash->{message} = "Thingy $id was deleted";
        $c->forward('/admin/things');  # App::C::Admin::things() : Local
    }

Then the relative links on the table end up like this, of course:

    /admin/thing/delete/234?o=date

instead of:

    /admin/things?o=date

I can't use $c->req->action or $c->req->match as that's the action
that matched the original request (/admin/thing/delete/234).  I
suppose I could redirect, but that's very ugly.

I originally was creating a $c->stash->{myself} entry for
self-referencing links in all my controllers.  Seems like the only way
since Catalyst can't really know what action is the "self".

I guess it's more than relative links.  If I create links using
$c->req->action forwarding screws that up, too.


-- 
Bill Moseley
moseley at hank.org




More information about the Catalyst mailing list