Search code examples
perlchainingcatalyst

multiple chain reuse in catalyst


I am a perl & catalyst newbie and I have been playing around with Catalyst::DispatchType::Chained and I am wondering if it is possible to allow for chained paths to be rewired in different patterns :

/hello/*/world/*
/world/*/hello/*
/hello/*
/world/*

or do you have to have a uniquely defined endpoint for each path ?


Solution

  • I think this would be two chains with two endpoints:

    sub hello : PathPart("hello") : CaptureArgs(1) {# /hello/*/...} 
    #-> 
    sub world : chained "/hello" : PathPart("world") : Args(1) {# /hello/*/world/*}
    
    sub world_base : PathPart("/world") : CaptureArgs(1) {# world/*/...}
    #->
    sub hello_world chained "world_base" PathPart("hello") Args(1) {# /world/*/hello/*}
    

    However, if they both do the same thing, just forward to a method that does what you want in both methods, I would suggest.

    Forwarding can be weird across controllers, I usually have a method for that in my Main.pm. If you need further help with that, feel free to ask.

    Be a bit careful about "chains" and "paths": A chain always needs an endpoint, that is, a sub Chained with only Args( ) at the end, as above.

    Paths are matched via PathPart.

    for example

    sub base Pathpart( "" ) : CaptureArgs(1) {}
    
    sub hello Chained("base"): PathPart("hello") : Args(0){ 
    # this is an endpoint
    # path is /*/hello
    
    sub hello_world : Chained("base") : PathPart("hello") : Args(1){
    # another end to the chain started at base
    # path is /*/hello/*
    
    sub again : Chained("hello") : PathPart("") :CaptureArgs(1) {
    # path is /*/hello/*/ ...
    sub hello_universe : Chained("hello") : Pathpart("universe") : Args(1){
    # another endpoint to another chain
    # path is /*/hello/*/universe/*
    

    Hope this helps.