[Catalyst] Generate a URL for a given action

John Napiorkowski jjn1056 at yahoo.com
Thu Aug 31 17:28:33 CEST 2006


Thanks for pointing me in the right direction regarding the chaining stuff.  I'm still struggling a bit with the question about getting the uri for an action.  

[quote]
> $c->response->redirect($c->uri_for('templates'));
> 
> Would this work correctly even if this controller ends up 
> deeply nested?

`$c->response->redirect($c->uri_for('templates'))' should work, since it's a
path relative to the current controller. The uri_for method should prepend
the URI with your app's base URL and the path to the controller, so that
should be all you need.
[/quote]

Turns out for some reason that the "$c->response->redirect($c->uri_for('templates'))" ends up trying to redirect me to /templates/templates.  But in any case that's not exactly what I am trying to do.  What I want is that when the edit action is called if the form is POSTed and validated to redirect to the view action of that particular template.  So I might have a template edit form at url:

/templates/mytemplate/edit

the form posts back to itself and if everything is okay I want to redirect to the view action which is chained to /templates/* and would look like this for the given example:

/templates/mytemplate

Now, I can do this with the following bit of code in my working system:

$c->response->redirect( $c->uri_for("/templates/".$template_rs->title) );

because I already have the title in the database resultset and I know right now this controller is rooted to the start of the web server.  What I would really like to know is if there is a programmatic way to get this stuff.  I know I can get the capture string via: $c->request->captures->[$level].  Ideally I wouldn't have to hard code so much controller specific stuff here.

What would be great is if there was a way to get the uri for a given action in a controller if you know the action name only.  What would be even better is if this method was smart about the current context to account for chained actions and returned the uri with the current captured args.  It seems the $c->uri_for() can do at least the first part of this, but I am probably not using it correctly.  Any thoughts or advice would be appreciated.  I actually have time to work on making this if this is something people would like to see and it doesn't currently exist.

Thanks!
John



----- Original Message ----
From: Justin Guenther <jguenther at gmail.com>
To: John Napiorkowski <jjn1056 at yahoo.com>; The elegant MVC web framework <catalyst at lists.rawmode.org>
Sent: Wednesday, August 30, 2006 10:57:27 PM
Subject: RE: [Catalyst] Generate a URL for a given action

> -----Original Message-----
> From: catalyst-bounces at lists.rawmode.org 
> [mailto:catalyst-bounces at lists.rawmode.org] On Behalf Of John 
> Napiorkowski
> Sent: August 30, 2006 8:08 PM
> To: catalyst at lists.rawmode.org
> Subject: [Catalyst] Generate a URL for a given action
> 
> Hi,
> 
> I'm sure this is a no brainer but I just can't find it 
> anywhere.  Basically what I am trying to find out is if there 
> is a programmatic way to discover the url for a given action 
> in controller just by using the action name.  Here is the 
> example I am trying to deal with:

You've got it backwards. In your example the `list', `template' and `view'
actions all map to `/templates'.

You should have something like:

  package MyApp::Controller::Templates;

  sub template :Chained('/') :PathPart('') :CaptureArgs(1) {...}
  sub list :Chained('template') :Args(0) {...}
  sub view :Chained('template') :Args(0) {...}

See
http://search.cpan.org/~mramberg/Catalyst-Runtime-5.7001/lib/Catalyst/Dispat
chType/Chained.pm#Introduction for a good example.

>

 *snip*

> Basically I'd like to replace the hardcoded path in the 
> redirect("/templates") since I am not too sure where this app 
> will eventually sit in the web hierarchy.  Now, the docs for 
> $c->get_uri says you can pass an action to it to get the url, 
> which seems to me what I want.  If I did:
> 
> $c->response->redirect($c->uri_for('templates'));
> 
> Would this work correctly even if this controller ends up 
> deeply nested?

`$c->response->redirect($c->uri_for('templates'))' should work, since it's a
path relative to the current controller. The uri_for method should prepend
the URI with your app's base URL and the path to the controller, so that
should be all you need.
 
> --john
> 
> 
> 
> _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: 
> http://www.mail-archive.com/catalyst@lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/









More information about the Catalyst mailing list