[Catalyst] URL mapping relative to Controller name space.

Bill Moseley moseley at hank.org
Wed Aug 10 15:42:30 CEST 2005


On Sat, Jul 30, 2005 at 08:21:36AM -0700, Bill Moseley wrote:
> 
> Is there a reason the default action isn't "Local"?

Just to follow up:

I now add to my base application class the following:

    # Set "myself" stash to controller
    # and shift off path names leaving just arguments after controller
    # name when default action is called within the controller.

    sub set_myself {
        my ( $c, $package ) = @_;
        $package =~ s/^.+::C:://;
        $package =~ s[::][/]g;
        $c->stash->{myself} = '/' . lc $package;


        return unless $c->req->action eq 'default';
        my @paths = split m!/!, $package;
        splice @{$c->req->arguments}, 0, @paths;
    }

Then in each controller I have:

    sub auto : Private {
        my ($self, $c ) = @_;
        $c->set_myself( __PACKAGE__ );
        return 1;
    }

So, that gives me a "myself" stash variable to use in templates to
make links back to the controller.

  <a href="[% myself %]/list">Call list action</a>

It also shifts the arguments passed to the default() action to be just
the path segments after the path to the controller. This makes
default() act like a Local action with regards to what parameters are
passed to it.

So in controller App::C::Foo::Bar I can handle a path
/foo/bar/<table>/<id> in the default handler.  default will get passed
$table, $id as parameters.  Normally, it would get passed ('foo',
'bar', $table, $id).

Anyone think of how that change might bite me later on?  I assume
there's a reason the default action in a controller acts the way it
does.  Perhaps default() is used for other functions I'm not
considering.



-- 
Bill Moseley
moseley at hank.org




More information about the Catalyst mailing list