Catalyst::Plugin::RedirectTo - Easier redirects to action objects or private paths.


Use the plugin in your application class:

    package MyApp;
    use Catalyst 'RedirectTo';


Then you can use it in your controllers:

    package MyApp::Controller::Example;

    use base 'Catalyst::Controller';

    sub does_redirect_to :Local {
      my ($self, $c) = @_;
      $c->redirect_to( $self->action_for('target'), [100] );

    sub does_redirect_to_action :Local {
      my ($self, $c) = @_;
      $c->redirect_to_action( 'target', [100] );

    sub target :Local Args(1) {
      my ($self, $c, $id) = @_;
      $c->response->body("This is the target action for $id");


Currently if you want to setup a redirect in Catalyst to an existing action the proper form is somewhat verbose:

        $c->controller(...)->action_for(...), \@args, \%q

Which is verbose enough that i probably encourages people to do the wrong thing and use a hard coded link path in the redirect request. This might later bite you if you need to change your controllers and URL hierarchy.

Also, for historical reasons the default redirect code is 302, which is considered a temporary redirect, rather than 303 which is a better default for the common use case of a form POST that generates a new resource. Which means to do the right thing you really need:

        $c->controller(...)->action_for(...), \@args, \%q

This plugin seeks to relieve some of the effort involved in doing the right thing. It does this by creating a context method which encapulates the redirect response setup (and sets a 303 by default, since that is the common case today) with a call to 'uri_for' (or 'uri_for_action'). So instead of the above you can just do:

    $c->redirect_to($c->controller(...)->action_for(...), \@args, \%q);

or even:

    $c->redirect_to_action('controller/action', \@args, \%q);

Which hopefully is a good encapsulation of 'the right thing to do'!

NOTE: Please be aware that setting up a redirect does not automatically detach or complete the action. You still should either return the redirect or call 'detach' if you want to stop action processing.


This plugin adds the following methods to your context

redirect_to ($action_obj, \@args, \%query, \$code)


    $c->redirect_to( $action_obj, \@args, \%query, \$code);

Is shorthand for:

      $c->uri_for( $action_obj, \@args, \%query), $code);

$code will default to 303 (not 302 as does $c->res->redirect) as this is commonly supported in modern browsers, so unless you have a specific need for an alternative response code, you should be able to just leave it off.

For example:

  $c->redirect_to($self->action_for($action_name), \@args);

Basically all the arguments to this method will be sent to ->uri_for, unless the last argument is a scalar ref, in which case it will be used to set the HTTP status code. $code must be '3xx' (a valid current or future redirect status).

Does not detach or return the current action (just like the existing method)!

NOTE: Please notice that if you want to set a status code other than 303, that code must be added to the argument list as a scalar ref. This is needed to distinguish from an argument that gets passed to 'uri_for'.


Same as 'redirect_to' but submits the arguments to 'uri_for_action' instead. Please NOTE that if you also install Catalyst::Plugin::URI we will use that for action resolution (supports named Actions).


John Napiorkowski


Catalyst, Catalyst::Response


Copyright 2018, John Napiorkowski

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.