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!

gawbul
  • 1,207
  • 12
  • 20

2 Answers2

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.

RET
  • 9,100
  • 1
  • 28
  • 33
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');
}
monken
  • 116
  • 1
  • 4