Mailing List Archive

Calling controller/action based on parameter
 Hi.

I've been struggling with the best way to accomplish a somewhat simple task: direct to a specific controller/action based on a code in the uri.

My site is (going to be) a comprehensive entertainment database covering movies, tv shows, books, comics, graphic novels, and even games and posters.  Each media type has it's own code...

+-----+--------------------+-------------+
| id  | name               | description |
+-----+--------------------+-------------+
| mov | Movie              | NULL        |
| tvs | TV Series          | NULL        |
| hbo | HBO                | NULL        |
| mgz | Magazine           | NULL        |
| bok | Book               | NULL        |
| bbd | Billboard          | NULL        |
| gcd | Greeting Card      | NULL        |
| ptr | Poster             | NULL        |
| pcd | Post Card          | NULL        |
| web | Website            | NULL        |
| tvm | TV Movie           | NULL        |
| stv | Straight to Video  | NULL        |
| tms | TV Miniseries      | NULL        |
| tve | TV Episode         | NULL        |

and so on.  While not a "media type" I also code persons as "prn".

So my url's currently look like /media_type/id ...
Movies ...
Tron:  http://www.pausetosee.com/mov/movez9zbefv1fzaq
Event Horizon:  http://www.pausetosee.com/mov/mov1ufub4zg1mp2j

TV Episodes ...
Sons of Anarchy : The Pull:  http://www.pausetosee.com/tve/tve28or7cin210v3
Chicago Fire : A Problem House:  http://www.pausetosee.com/tve/tveeoz58rafdccn5

Persons ...
Robert Downey Jr.:  http://www.pausetosee.com/person/prniffqosbaj0h2n
Ron Perlman:  http://www.pausetosee.com/person/prnn7ivdbdq8808p


What I'd like to do is have the system direct to the proper controller and action based on the 3 character code in the ID ...

Tron:  http://www.pausetosee.com/movez9zbefv1fzaq
Event Horizon:  http://www.pausetosee.com/mov1ufub4zg1mp2j

TV Episodes ...
Sons of Anarchy : The Pull:  http://www.pausetosee.com/tve28or7cin210v3
Chicago Fire : A Problem House:  http://www.pausetosee.com/tveeoz58rafdccn5

Persons ...
Robert Downey Jr.:  http://www.pausetosee.com/prniffqosbaj0h2n
Ron Perlman:  http://www.pausetosee.com/prnn7ivdbdq8808p


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?

Any help is appreciated.

Thanks,

bill
http://wbhauck.com
Re: Calling controller/action based on parameter [ In reply to ]
I'd try a regexp in my begin action.
But I'd first look at something more properly restful
http://www.pausetosee.com/prn/n7ivdbdq8808p
You can also do pre-dispatching with rewrite rules on your webserver.
in apache
Rewrite Engine On
RewriteRule ^/prn(.*)
/http://localhost:3003/controllers/people/n7ivdbdq8808p [P]


On 10/03/2013 09:41 PM, bill hauck wrote:
>
> I've been struggling with the best way to accomplish a somewhat simple
> task: direct to a specific controller/action based on a code in the uri.
>


_______________________________________________
List: Catalyst@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/
Re: Calling controller/action based on parameter [ In reply to ]
Hit send too soon
RewriteRule ^/prn(.*) /http://localhost:3003/controllers/people/$1 [P]



On 10/03/2013 10:13 PM, John Karr wrote:
> I'd try a regexp in my begin action.
> But I'd first look at something more properly restful
> http://www.pausetosee.com/prn/n7ivdbdq8808p
> You can also do pre-dispatching with rewrite rules on your webserver.
> in apache
> Rewrite Engine On
> RewriteRule ^/prn(.*)
> /http://localhost:3003/controllers/people/n7ivdbdq8808p [P]
>
>
> On 10/03/2013 09:41 PM, bill hauck wrote:
>>
>> I've been struggling with the best way to accomplish a somewhat
>> simple task: direct to a specific controller/action based on a code
>> in the uri.
>>
>


_______________________________________________
List: Catalyst@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/
Re: Calling controller/action based on parameter [ In reply to ]
> 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




_______________________________________________
List: Catalyst@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/
Re: Calling controller/action based on parameter [ In reply to ]
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@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.

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





_______________________________________________
List: Catalyst@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/

_______________________________________________
List: Catalyst@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/