Difference between uri_for and uri_for_action
Asked Answered
C

3

6

What is the difference between $c->uri_for and $c->uri_for_action methods of Catalyst.

Which one to use? And why?

Crista answered 27/3, 2012 at 7:5 Comment(0)
C
1

I found below difference between $c->uri_for and $c->uri_for_action

Consider

Othercontroller.pm

__PACKAGE__->config(namespace => 'Hello');

.  
.   
.  

sub bar: Local{

$c->res->redirect($c->uri_for('yourcontroller/method_name'));

$c->res->redirect($c->uri_for_action('yourcontroller/method_name'));

}

Yourcontroller.pm

sub method_name: Local{

print "In Yourcontroller:: method_name"

}

In case of $c->uri_for the url changes to

http://localhost:3000/Hello/yourcontroller/method_name

However for $c->uri_for_action the url changes to

 http://localhost:3000/yourcontroller/method_name

So the namespace gets added in case of uri_for.

Crista answered 27/3, 2012 at 12:5 Comment(0)
H
5

@Devendra I think your examples could be somehow misleading if someone reads them.

uri_for expects path (and not action). It return an absolute URI object, so for example it's useful for linking to static content or in case you don't expect your paths to change.

So for example, let say you've deployed your application on domain example.com and subdir abc (example.com/abc/): $c->uri_for('/static/images/catalyst.png') would return example.com/abc/static/images/catalyst.pn, or for example: $c->uri_for('/contact-us-today') would return example.com/abc/contact-us-today. If you decide later to deploy your application under another subdirectory or at / you'll still end up with correct links.

Let say that your contact-us action looks like: sub contact :Path('/contact-us-today') :Args(0) {...} and you decide later that /contact-us-today should become just /contact-us. If you've used uri_for('/contact-us-today') you'll need to find and change all lines which points to this url. However you can use $c->uri_for_action('/controller/action_name') which will return the correct url.

Hypodermis answered 27/3, 2012 at 12:50 Comment(4)
uri_for also works with action i.e. $c->uri_for('/controller/action_name'); will also work.That's the issue actually.I couldn't find any difference between their (uri_for and uri_for_action) behavior/working except the namespace. Please check and reply.Crista
Though you are right on the point about using method_name and not to match direct url.Crista
Reading the docs seems like that :) I've just never used it like that. Sorry if I've confused youHypodermis
$c->uri_for_action is only a small wrapper around $c->uri_for that fails if the path does not resolve to a known action. If the 1st argument is an Action object $c->uri_for is called anyway and can be used in the first place. You can find that in lib/Catalyst.pm.Betterment
F
4

dpetrov_ in #catalyst says:

If the paths are likely to change, uri_for_action is better idea.

Franklin answered 27/3, 2012 at 10:25 Comment(0)
C
1

I found below difference between $c->uri_for and $c->uri_for_action

Consider

Othercontroller.pm

__PACKAGE__->config(namespace => 'Hello');

.  
.   
.  

sub bar: Local{

$c->res->redirect($c->uri_for('yourcontroller/method_name'));

$c->res->redirect($c->uri_for_action('yourcontroller/method_name'));

}

Yourcontroller.pm

sub method_name: Local{

print "In Yourcontroller:: method_name"

}

In case of $c->uri_for the url changes to

http://localhost:3000/Hello/yourcontroller/method_name

However for $c->uri_for_action the url changes to

 http://localhost:3000/yourcontroller/method_name

So the namespace gets added in case of uri_for.

Crista answered 27/3, 2012 at 12:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.