1package CGI::Application::Plugin::Redirect; 2 3use strict; 4use vars qw($VERSION @EXPORT); 5 6$VERSION = '1.00'; 7 8require Exporter; 9 10@EXPORT = qw( 11 redirect 12); 13 14sub import { goto &Exporter::import } 15 16sub redirect { 17 my $self = shift; 18 my $location = shift; 19 my $status = shift; 20 21 # The eval may fail, but we don't care 22 eval { 23 $self->run_modes( dummy_redirect => sub { } ); 24 $self->prerun_mode('dummy_redirect'); 25 }; 26 27 if ($status) { 28 $self->header_add( -location => $location, -status => $status ); 29 } else { 30 $self->header_add( -location => $location ); 31 } 32 $self->header_type('redirect'); 33 return; 34} 35 361; 37__END__ 38 39=head1 NAME 40 41CGI::Application::Plugin::Redirect - Easy external redirects in CGI::Application 42 43 44=head1 SYNOPSIS 45 46 package My::App; 47 48 use CGI::Application::Plugin::Redirect; 49 50 sub cgiapp_prerun { 51 my $self = shift; 52 53 if ( << not logged in >> ) { 54 return $self->redirect('login.html'); 55 } 56 } 57 58 sub byebye { 59 my $self = shift; 60 61 return $self->redirect('http://www.example.com/', '301 Moved Permanently'); 62 } 63 64 65=head1 DESCRIPTION 66 67This plugin provides an easy way to do external redirects in CGI::Application. 68You don't have to worry about setting headers or worrying about return types, as 69that is all handled for you. 70 71C<redirect> does an external redirect, which means that the browser will receive 72a command to load a new page, and a fresh request will come in. If you just want to 73display the results of another runmode within the same module, then it is often 74sufficient to use the C<forward> method in L<CGI::Application::Plugin::Forward> instead. 75 76 77=head1 METHODS 78 79 80=head2 redirect($url, $status) 81 82Interupt the current request, and redirect to an external URL. If 83you happen to be inside a prerun method when you call this, the 84current runmode will automatically be short circuited so that it 85will not execute. As soon as all prerun method have finished, 86the redirect will happen without the runmode being executed. 87 88The $status paramater is optional as the CGI module will default to something 89suitable. 90 91 return $self->redirect('http://www.example.com/'); 92 93 - or - 94 95 return $self->redirect('http://www.example.com/', '301 Moved Permanently'); 96 97 98=head1 SEE ALSO 99 100L<CGI::Application>, L<CGI::Application::Plugin::Forward>, perl(1) 101 102 103=head1 AUTHOR 104 105Cees Hek <ceeshek@gmail.com> 106 107 108=head1 LICENCE AND COPYRIGHT 109 110Copyright (c) 2005, Cees Hek. All rights reserved. 111 112This module is free software; you can redistribute it and/or modify 113it under the same terms as Perl itself. 114 115 116=head1 DISCLAIMER OF WARRANTY 117 118BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED ORIMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. 119 120IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSESSUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 121 122=cut 123