[Catalyst] URL mapping relative to Controller name space.

David Storrs dstorrs at dstorrs.com
Sat Jul 30 19:46:05 CEST 2005


On Jul 30, 2005, at 11:21 AM, Bill Moseley wrote:

> On Sat, Jul 30, 2005 at 09:22:42AM +0200, Danijel Milicevic wrote:
>
>>> I want a single controller to handle a number of tables (say, it's a
>>> general CRUD handler for a collection of tables) with paths like:
>>>
>>>     http://localhost:3000/admin/crud/<table>/list
>>>     http://localhost:3000/admin/crud/<table>/edit/<id>
>>>     http://localhost:3000/admin/crud/<table>/do_edit/<id>
>>
>> Sounds to me like a perfect example to use a a Regex action.[...]
>
> I thought about using Regex, but couldn't make it fit.  Perhaps I'm
> not understanding all it can do.  For one thing, I want to be able to
> handle no table:
>
>     http://localhost:3000/admin/crud/

>> Better solution would probably be using a keyword
>> (like you do with crud?) and match the tablename and action after the
>> occurance of your keyword.
>
> I suppose that's the best way -- just adds an extra level to my path.

Caveats:  I'm still pretty raw at Catalyst, so I may have this  
totally wrong...and I may not completely get what you're looking for.

Would this do what you want? (untested):

     package App::C::<WHATEVER>;

     sub table_dispatcher : Regex('^admin/crud/([\w/]*)') {
         my ($self, $c) = @_;

         #  This could all be done in the Regex above, but this seems  
cleaner
         #
         my ($table, $command, $id) = split /\//, $c->req->snippets[0];

         $table = 'list_tables' unless $table;
         $c->forward($table, [$command, $id]);
    }

     sub users : Private {
         #  Deal with the 'users' table

         my ($self, $c, $args) = @_;
         my ($command, $id) = @$args;
     }


Two things I assumed that may be wrong:  I think $args comes in as an  
array ref, not as array elements in @_.  Also, I think $c->req- 
 >snippets is an arrayref (as opposed to hashref, scalar, etc).   
Also, I assumed you would want separate functions to handle each  
table.  If they all get treated the same, then you don't need the  
redispatch.

--Dks




More information about the Catalyst mailing list