[Catalyst] Serving server-dependent static content

mreece at vinq.com mreece at vinq.com
Sun Aug 12 20:45:39 GMT 2007

Catalyst::Plugin::Static::Simple can more or less do what you want.


note this example provides a sub-ref in the static include path, and that
sub receives $c, and thus can inspect $c->request or $c->config or
whatever else it needs to decide which path(s) to include.

however, ultimately i had issues with this plugin because it really wants
to serve up anything with any extension (but for an exception list),
rather than only anything prefixed with /static/.  and for what did match
/static/, it provided no easy way to shift off that path part, so it ended
up looking in /path/static/static/ unless i made overrides elsewhere to
try to fix up the $c->req->path myself (or risk exposing the parent
directory, shudder).

finally, you will want to solve this via your webserver config.  however,
sometimes you don't want to rely on apache during development (perhaps you
favor myapp_server.pl).

in my case, i wanted mason-esque inheritance for static files, so if a
cobrand does not provide an image it is served from the default
collection.  for production, it is done with mod_rewrite rules for each
virtual host.

but for development, i took my favorite bits out of
Catalyst::Plugin::Static::Simple and created a Myapp::Controller::static
that handles it.  overall, i think the behavior works better as a
controller than a plugin, so usual action methods can be used to control
routes for 'static' files, rather than a complex and unsatisfying array of
$c->config options.  but for now, that means rolling your own.


> Good afternoon - I am working on a Catalyst application that serves
> dynamic
> website content for multiple sites, and I've run into some difficulty
> about
> static content.  Each website has unique static content (images, css, rss)
> and I am trying to serve that static content from the site's unique
> directory.  I have found lots of documentation on how to serve all static
> content from a single directory, but not much on a servername-dependent
> directory.  For example:
> Request:  http://www.timjohnson.com/css/global.css
> Should be served with:  /var/www/html/sites/timjohnson.com/css/global.css
> I came close to making this happen just using the Alias directive in
> httpd.conf, but I couldn't get that to accept a variable, %{SERVER_NAME},
> in
> it.  If someone has a solution from the Apache side, that's fine but I
> suspect that it might be easier to do this with
> Catalyst::Plugin::Static::Simple, though I again have not figured out how
> to
> tell it to use a variable name when it creates the path the request is
> supposed to be mapped to.  I am sending this to the Catalyst list because
> I
> suspect it might be something other people have faced before using
> Catalyst.
> My setup is a recent version of Catalyst running on Apache 2.2 with
> mod_perl.  Any suggestions would be quite welcome - thank you very much.
> Peter Lytle

More information about the Catalyst mailing list