The difference between uri_for and uri_for_action

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

Which one to use? And why?

+4
source share
3 answers

I found below the 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 the case of $c->uri_for URL changes to

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

However, for $c->uri_for_action url changes to

  http://localhost:3000/yourcontroller/method_name 

So, the namespace is added in case of uri_for .

+1
source

dpetrov_ in # catal says:

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

+4
source

@Devendra I think your examples can be somehow deceiving if someone reads them.

uri_for expects a path (not an action). It returns an absolute URI object, therefore, for example, it is useful for binding to static content or in case you do not expect your paths to change.

So, let's say you deployed your application on the domain example.com and subdir abc (example.com/abc/): $c->uri_for('/static/images/catalyst.png') returned example.com/abc/static /images/catalyst.pn, or for example: $c->uri_for('/contact-us-today') will return example.com/abc/contact-us-today. If later you decide to deploy your application in another subdirectory or in / you will still get the correct links.

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

+4
source

Source: https://habr.com/ru/post/1403597/


All Articles