[Catalyst] Calling controller/action based on parameter
Richard Thomas
ret at mac.com
Sat Oct 19 06:56:54 GMT 2013
> 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.
cheers
RET
_______________________________________________________________
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
More information about the Catalyst
mailing list