[Catalyst] Relative links
Bill Moseley
moseley at hank.org
Thu Nov 10 00:17:41 CET 2005
On Wed, Nov 09, 2005 at 09:22:26PM +0000, Matt S Trout wrote:
> On Wed, Nov 09, 2005 at 01:02:08PM -0800, Bill Moseley wrote:
> > I originally was creating a $c->stash->{myself} entry for
> > self-referencing links in all my controllers. Seems like the only way
> > since Catalyst can't really know what action is the "self".
>
> As of 5.49, you can access the Catalyst::Action object for the current action
> via $c->action. Something like $c->action->attributes->{Path}[0] will work
> for any Path or Regex action.
Well, Catalys::Action will be useful.
In my template (e.g. for creating my table heading links) I have been
using:
USE u = URL('', c.req.parameters );
So I just changed to
USE u = URL(myself, c.req.parameters );
And then I can set "myself" as needed. Which can be:
$c->stash->{myself} = $c->base . $c->action->reverse;
Another useful Catalyst::Action is the ->namespace method. I already
had this in my base class auto method:
# Often want to link relative to the controller.
if ( $c->req->action ) {
my @segs = split(m!/!, $c->req->action );
pop @segs;
$c->stash->{controller} = join('/', '', @segs );
}
But can now be replaced by:
$c->stash->{controller} = $c->action->namespace;
This solves my link problem. Luckily, I've been using $controller in
my templates so my delete that forwards can now just do:
sub delete {
$c->stash->{controller} = '/admin';
$c->stash->{myself} = '/admin/things';
my ($self, $c, $id ) = @_;
DB::Thing->retrieve($id)->delete;
$c->stash->{message} = "Thingy $id was deleted";
$c->forward('/admin/things'); # App::C::Admin::things() : Local
}
And the links work. I knew I set those two variables up for a reason.
I wanted to avoid using a redirect because I like to be able to display that
message.
--
Bill Moseley
moseley at hank.org
More information about the Catalyst
mailing list