perlcatalyst

Difference between uri_for and uri_for_action


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

Which one to use? And why?


Solution

  • 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.