Is there a way to allow multiple chains to share the same endpoint in Catalyst?
Asked Answered
P

2

7

I'm a bit of a Catalyst newbie, and I'm trying to get multiple chains to access the same endpoint ('description' subroutine) e.g:

/object/fetch_by_id/*/description
/object/fetch_by_name/*/description
/object/fetch_by_xref/*/description

I don't want to have to write code for 3 separate endpoints for example and instead allow the endpoint to be shared between the three different chained actions. I am wrapping a backend API and in this example the object can be retrieved via different methods.

The ideal solution would be:

sub description : Chained('fetch_by_id','fetch_by_name','fetch_by_xref') PathPart('description') Args(0) ActionClass('REST') {
    # code here
}

Or I could write different description subs for each chain that all call the generic description subroutine, but any more elegant solutions would be great! Any help should be greatly appreciated!

Patrilocal answered 22/8, 2011 at 16:4 Comment(0)
P
5

Have you considered refactoring your existing subs to something like:

/object/fetch/id/*/description
/object/fetch/name/*/description
/object/fetch/xref/*/description

You might find you can solve both the end-point problem and reduce your existing code at the same time: have 'fetch' take two arguments: lookup-method and value, and chain description to the end.

Philosophize answered 23/8, 2011 at 0:0 Comment(0)
S
2

The catalyst way would be to use $c->forward

sub description : Chained('fetch_by_id') PathPart('description') Args(0) ActionClass('REST') {
    # code here
}

sub alias_1 : Chained('fetch_by_name') PathPart('description') Args(0) ActionClass('REST') {
    my ($self, $c) = @_;
    $c->forward('description');
}
Spumescent answered 22/8, 2011 at 23:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.