[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:

   http://dev.catalyst.perl.org/wiki/wikicookbook/urlpathprefixing

(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';

__PACKAGE__->setup();

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

    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) || '/';
    $c->request->path($new_path);

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

    return;
}

------

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
"/foo/some/path".



HTH,
Larry



More information about the Catalyst mailing list