Search code examples
perlmojolicious

How can I internally redirect with Mojo?


Catalyst supported redirects internal to the framework (forward and detach), and external (HTTP) redirects (res->redirect). Internal redirects essentially just redispatched to a new path,

Lets say I set up two paths,

sub register ( $self, $app, $conf ) {
    my $r = $app->routes;
    $r->any( 'foo',  => \&foo_baz );
    $r->any( 'bar',  => \&bar_baz );
}

How would I tell foo after it does some stuff, to internally redirect to bar?

sub foo_baz ($c) {
  ; stuff
  bar_baz($c)
}

Is there a better way than calling bar_baz($c) directly? These routes are declared in a Mojo Plugin.


Solution

  • Discussed on IRC, but: Mojolicious does not support the dynamic routing of internal redirects as Catalyst does. The common suggestion is to abstract out your code to avoid duplication while not directly calling another route. So instead of route A calling route B, both of them would call a helper or method to complete the common functionality using their current controller object.