Search code examples
perlcatalystchainingendpoint

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


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!


Solution

  • 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.