[Catalyst] RFC on how to structure controllers

Thomas Nagel me at thomasnagel.com
Tue Jun 13 13:41:04 CEST 2006


Gert Burger wrote:
> -Using the game as part of the url, eg 
> http://bla/games/ageofempires3/gamebooking, this will complicate the 
> controller's methods a bit as each method will be a regex.

The game-as-part-of-URL seem to be the cleanest approach to me:

Why not extend prepare_path globally with something like (untested):

sub prepare_path {
     my $c = shift;
     $c->NEXT::prepare_path(@_);

     my $base = $c->req->base;
     my $path = $c->req->path;

     if ($path =~ m!^(game1|game2)/!) {
         my $game = $1;
         $c->stash->{game} = $game;

         ## optionally rm $game from URL
         $path =~ s!^$game/!!;
         $c->req->path($path);
     }

     return;
}

Now game can be found in $c->stash->{game}

You my also overwrite the behavior of $c->uri_for by using a Plugin
(again untested):

package Catalyst::Plugin::GameContext;

use strict;
use Next;
use URI;

sub uri_for {
    my $c    = shift;
    my $uri  = URI->new( $c->NEXT::uri_for( @_ ) );

    $uri->path( $c->stash->{game} . $uri->path )
        unless $uri->path =~ m!/(?:css|i|js)/!;

    return $uri->as_string;
}

and include it with

use Catalyst qw(ConfigLoader DefaultEnd GameContext Static::Simple);


-Thomas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3287 bytes
Desc: S/MIME Cryptographic Signature
Url : http://lists.rawmode.org/pipermail/catalyst/attachments/20060613/ed2861d4/attachment.bin 


More information about the Catalyst mailing list