[Catalyst] Calling controller/action based on parameter

John Napiorkowski jjn1056 at yahoo.com
Mon Oct 21 16:11:45 GMT 2013

You could also try to encapsulate the match logic in an action class or role.  --jnap

On Saturday, October 19, 2013 1:58 AM, Richard Thomas <ret at mac.com> wrote:
> Any suggestions on how to do this?  I'm toying with a large switch statement in the root controller and visiting the appropriate controller/action based on the parameter.  Is this right?  Does Catalyst provide a way to accomplish this either easier or cleaner?

Hi Bill,

Rather than a big mess of a switch statement, I would be inclined to construct a hash that contains the rules for the mapping of the short-codes to the appropriate Controller/action, i.e.

my $codes = {
    'tms' => [ 'Controller1', 'action1' ],
    'tve' => [ 'Controler1', 'action2' ],
    'prn' => [ 'ControllerN', 'actionN' ],

then the control flow is more or less abstracted into a simple hash of rules:

if($args[0] =~ /^(\w{3})(\w{13})$/){ # separate media type from key
    my ($code, $key) = ($1, $2);
    if(my $ca = $codes->{$code}){
        $c->forward("MyApp::Controller::".$ca->[0], $ca->[1], [ $key, @args ]);
    else {

To be honest, I think this is the sort of use for which the 'Regex' action type shines, but I believe it's being deprecated.

If the programmers like each other, they play a game called "pair programming".
And if not, then the game is called "peer review".
- Anna Nachesa (@ashalynd)

Richard Thomas

List: Catalyst at lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

More information about the Catalyst mailing list