How can I introduce a regex action to match the first element in a Catalyst URI ?

Posted by RET on Stack Overflow See other posts from Stack Overflow or by RET
Published on 2011-06-25T02:51:04Z Indexed on 2011/06/27 8:22 UTC
Read the original article Hit count: 202

Filed under:
|

Background:

I'm using a CRUD framework in Catalyst that auto-generates forms and lists for all tables in a given database. For example: /admin/list/person or /admin/add/person or /admin/edit/person/3 all dynamically generate pages or forms as appropriate for the table 'person'. (In other words, Admin.pm has actions edit, list, add, delete and so on that expect a table argument and possibly a row-identifying argument.)

Question:

In the particular application I'm building, the database will be used by multiple customers, so I want to introduce a URI scheme where the first element is the customer's identifier, followed by the administrative action/table etc:

  • /cust1/admin/list/person
  • /cust2/admin/add/person
  • /cust2/admin/edit/person/3

This is for "branding" purposes, and also to ensure that bookmarks or URLs passed from one user to another do the expected thing.

But I'm having a lot of trouble getting this to work. I would prefer not to have to modify the subs in the existing framework, so I've been trying variations on the following:

sub customer : Regex('^(\w+)/(admin)$') {
    my ($self, $c, @args) = @_;
    #validation of captured arg snipped..
    my $path = join('/', 'admin', @args);
    $c->request->path($path);
    $c->dispatcher->prepare_action($c);
    $c->forward($c->action, $c->req->args);
}

But it just will not behave. I've used regex matching actions many times, but putting one in the very first 'barrel' of a URI seems unusually traumatic. Any suggestions gratefully received.

© Stack Overflow or respective owner

Related posts about perl

Related posts about catalyst