johnnywang has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm using CGI::Application for a web application. The app is organized in several modules, each has its own instance scripts. I'm wondering how to do a server side forwarding from one module's runmode to another's. For example, when the user requests /cgi-bin/foo.pl?rm=mode1, I'd like the runmode mode1 to do some processing and then forward the request to /cgi-bin/bar.pl?rm=mode2 (e.g., keeping all parameters, environments, etc.). I'd like to avoid redirect, which involves the browser.
  • Comment on CGI::Application and server side request forwarding

Replies are listed 'Best First'.
Re: CGI::Application and server side request forwarding
by friedo (Prior) on Aug 06, 2005 at 02:23 UTC
    If you're using Apache, you can make it do an internal redirect by sending a Location header with an absolute (but local) path. For example:

    package Foo; use base 'CGI::Application'; sub setup { my $self = shift; $self->start_mode('mode1'); $self->run_modes( mode1 => 'mode1' ); } sub mode1 { my $self = shift; $self->header_add( -location => '/bar.pl?rm=mode2' ); } 1;
Re: CGI::Application and server side request forwarding
by cees (Curate) on Aug 05, 2005 at 23:48 UTC

    There is no clean way of doing an internal redirect to another application module like that. I would think that if these two runmodes are related enough to share the same parameters, then perhaps they should be in the same modules.

    If you do not want to put them in the same module, you may be able to solve the problem with inheritance. Make module Foo inherit from module Bar, and then Foo will automatically have access to both runmode 'mode2' and 'mode1', so you can do a simple internal redirect from mode1 to mode2 using 'return $self->mode2()'.