diff options
Diffstat (limited to 'lib/Narwhal/Component/Redirect.pm')
-rw-r--r-- | lib/Narwhal/Component/Redirect.pm | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/Narwhal/Component/Redirect.pm b/lib/Narwhal/Component/Redirect.pm new file mode 100644 index 0000000..58a8478 --- /dev/null +++ b/lib/Narwhal/Component/Redirect.pm @@ -0,0 +1,31 @@ +package Narwhal::Component::Redirect; +use Moose; + +sub permanent { + my $self = shift; + my ($req) = @_; + my $res = $req->new_response(301); + $res->location($self->_get_location($req)); + return $res; +} + +sub temporary { + my $self = shift; + my ($req) = @_; + my $res = $req->new_response(302); + $res->location($self->_get_location($req)); + return $res; +} + +sub _get_location { + my $self = shift; + my ($req) = @_; + my $to = $req->env->{'plack.router.match'}->route->defaults->{to}; + die "must supply a location to redirect to" unless $to; + return $to; +} + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; |