[Catalyst] offset the URI of an existing Cat App

Larry Leszczynski larryl at emailplus.org
Tue Jul 7 15:23:17 GMT 2009

Hi Ian -

> I have always written Cat Apps so they start at the '/' URI but now I 
> have been asked to 'offset' one so that:-
> /        becomes /foo
> /user    becomes /foo/user
> /admin/1 becames /foo/admin/1

One approach is to modify $c->prepare_path, similar to:


(The example was written for Catalyst 5.7 and will work with 5.8, but
there's probably a Moose-ier way to do the same in 5.8...)

Something like:


package MyApp;

use Catalyst::Runtime '5.70';
use base 'Catalyst';


sub prepare_path
    my $c = shift;

    my @path_chunks = split m[/], $c->request->path, -1;

    # Pull prefix off beginning of request path:
    my $prefix = shift @path_chunks;

    # Create modified request path from any remaining path chunks:
    my $new_path = join('/', @path_chunks) || '/';

    # Modify the path part of the request base
    # to include the path prefix:
    my $base = $c->request->base;
    $base->path($base->path . "$prefix/");



So if the url was "/foo/admin/1", the controllers will see a request for
"/admin/1".  Also, $c->uri_for('some/path') will generate


More information about the Catalyst mailing list