[Catalyst] Mapping another site to a subset of a Catalyst application

Brian Kirkbride brian.kirkbride at deeperbydesign.com
Thu Jun 7 19:34:21 GMT 2007


Brian Kirkbride wrote:
> Hello all,
> 
> I've got an Catalyst webapp that has several areas:
> 
> /admin/...
> /manage/...
> /client/...
> 
> All of these are accessible from one site:
> 
> mysite.com/admin
> mysite.com/manage
> mysite.com/client
> 
> Now, here's the tricky part.  I'd really like to have a proxy or
> URL-rewriting setup that allowed for:
> 
> client1.com/abc (handled by /client/abc)
> client2.com/abc (handled by /client/abc)
> client3.com/xyz (handled by /client/xyz)
> 
> I'm currently using FastCGI and it's not a problem to map multiple
> virtual hosts to the Catalyst app.  I can even setup Apache to do
> an Alias / /path/to/myapp_fastcgi.pl/client/, which starts to do what
> I want.  The problem is that $c->uri_for will always return something
> like http://client1.com/client/... for any action I need the URI to.
> 
> I'm sure there is a better way to do this?  I suppose that I could
> override uri_for() to strip ^/client if the HTTP_HOST is not
> mysite.com but that seems ugly.
> 
> Thanks in advance!
> 
> Best,
> Brian Kirkbride

Replying to myself, comments welcome.

I've got a suitable workaround hacked up right now with the follow 
code in MyApp.pm:

sub prepare_action {
   my $c = shift;

   my $host = $c->req->uri->host;
   if ($host !~ /^mysite\.com$/i) {
     $c->{_client_site} = 1;
     my $path = $c->req->uri->path;
     if ($path !~ /^clients/) {
       $c->req->path("clients$path");
     }
   }

   return $c->next::method(@_);
}

sub uri_for {
   my $c = shift;
   my $uri = $c->next::method(@_);

   if ($c->{_client_site}) {
     (my $path = $uri->path) =~ s|^/clients||;
     $uri->path($path);
   }
   return $uri;
}


My first thought is that this prevents the app from having a base URI 
other than /, but that could probably be fixed with some clever 
$c->req->base checks in the regexes.

Am I missing anything important?  Something that might come back to 
haunt me later on?

Thanks!
- Brian



More information about the Catalyst mailing list