[Catalyst] URL mapping relative to Controller name space.

Bill Moseley moseley at hank.org
Sat Jul 30 17:21:36 CEST 2005


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. Write a
> proper Regex, something better than this:
> 
> sub match : Regex('([^/]*)/([^/]*)$') {
>     my ( $self, $c ) = @_;
>     my ( $table, $action ) = $c->req->snippets;
>     ...
> }

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/

which would display a list of tables that can be edited by this
controller.  So that wouldn't match the regex (although I could still
use a default handler).  

But, according to Manual::Intro to have variable arguments at the end
of the URL I need to anchor the regex to the start of the path --
which is exactly what I was trying to avoid.

> 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.


I guess where I'm having a problem is how Catalyst is setting up
"action" and "arguments" when the default handler is called.

    package MyApp::C::Admin::Lookup;
    sub tables : Local {
    }

Then a request has the following in $c->request

    http://localhost:3000/admin/lookup/tables/Keyword/edit/1

       'path' => 'admin/lookup/tables/Keyword/edit/1',
       'action' => 'admin/lookup/tables',
       'arguments' => [
                        'Keyword',
                        'edit',
                        '1'
                      ]

Which is great.  I can use "/$action as my base url in templates and I
have the correct arguments.

But, if I catch something in default instead then I can't really
figure out where I am and what are my parameters and what is the
"action" that will bring me back to this controller.  I guess I'm
looking for something analogous to $ENV{SCRIPT_NAME}.

    http://localhost:3000/admin/lookup/Keyword/edit/1

       'path' => 'admin/lookup/Keyword/edit/1',
       'action' => 'default',
       'arguments' => [
                        'admin',
                        'lookup',
                        'Keyword',
                        'edit',
                        '1'
                      ]

Is there a reason the default action isn't "Local"?  That is, the
request would instead look like:

    package MyApp::C::Admin::Lookup;
    sub default : Private {
        [...]
    }

       'path' => 'admin/lookup/Keyword/edit/1',
       'action' => 'admin/lookup',
       'arguments' => [
                        'Keyword',
                        'edit',
                        '1'
                      ]



-- 
Bill Moseley
moseley at hank.org




More information about the Catalyst mailing list