CGI-Application
view release on metacpan or search on metacpan
lib/CGI/Application.pm view on Meta::CPAN
$webapp->prerun_mode('new_run_mode');
The prerun_mode() method is an accessor/mutator which can be used within
your cgiapp_prerun() method to change the run mode which is about to be executed.
For example, consider:
# In WebApp.pm:
package WebApp;
use base 'CGI::Application';
sub cgiapp_prerun {
my $self = shift;
# Get the web user name, if any
my $q = $self->query();
my $user = $q->remote_user();
# Redirect to login, if necessary
unless ($user) {
$self->prerun_mode('login');
}
}
In this example, the web user will be forced into the "login" run mode
unless they have already logged in. The prerun_mode() method permits
a scalar text string to be set which overrides whatever the run mode
would otherwise be.
The use of prerun_mode() within cgiapp_prerun() differs from setting
mode_param() to use a call-back via subroutine reference. It differs
because cgiapp_prerun() allows you to selectively set the run mode based
on some logic in your cgiapp_prerun() method. The call-back facility of
mode_param() forces you to entirely replace CGI::Application's mechanism
for determining the run mode with your own method. The prerun_mode()
method should be used in cases where you want to use CGI::Application's
normal run mode switching facility, but you want to make selective
changes to the mode under specific conditions.
B<Note:> The prerun_mode() method may ONLY be called in the context of
a cgiapp_prerun() method. Your application will die() if you call
prerun_mode() elsewhere, such as in setup() or a run mode method.
=head2 Dispatching Clean URIs to run modes
Modern web frameworks dispense with cruft in URIs, providing in clean
URIs instead. Instead of:
/cgi-bin/item.cgi?rm=view&id=15
A clean URI to describe the same resource might be:
/item/15/view
The process of mapping these URIs to run modes is called dispatching and is
handled by L<CGI::Application::Dispatch>. Dispatching is not required and is a
layer you can fairly easily add to an application later.
=head2 Offline website development
You can work on your CGI::Application project on your desktop or laptop without
installing a full-featured web-server like Apache. Instead, install
L<CGI::Application::Server> from CPAN. After a few minutes of setup, you'll
have your own private application server up and running.
=head2 Automated Testing
L<Test::WWW::Mechanize::CGIApp> allows functional testing of a CGI::App-based project
without starting a web server. L<Test::WWW::Mechanize> could be used to test the app
through a real web server.
Direct testing is also easy. CGI::Application will normally print the output of it's
run modes directly to STDOUT. This can be suppressed with an environment variable,
CGI_APP_RETURN_ONLY. For example:
$ENV{CGI_APP_RETURN_ONLY} = 1;
$output = $webapp->run();
like($output, qr/good/, "output is good");
Examples of this style can be seen in our own test suite.
=head1 PLUG-INS
CGI::Application has a plug-in architecture that is easy to use and easy
to develop new plug-ins for.
=head2 Recommended Plug-ins
The following plugins are recommended for general purpose web/db development:
=over 4
=item *
L<CGI::Application::Plugin::Redirect> - is a simple plugin to provide a shorter syntax for executing a redirect.
=item *
L<CGI::Application::Plugin::ConfigAuto> - Keeping your config details in a separate file is recommended for every project. This one integrates with L<Config::Auto>. Several more config plugin options are listed below.
=item *
L<CGI::Application::Plugin::DBH> - Provides easy management of one or more database handles and can delay making the database connection until the moment it is actually used.
=item *
L<CGI::Application::Plugin::FillInForm> - makes it a breeze to fill in an HTML form from data originating from a CGI query or a database record.
=item *
L<CGI::Application::Plugin::Session> - For a project that requires session
management, this plugin provides a useful wrapper around L<CGI::Session>
=item *
L<CGI::Application::Plugin::ValidateRM> - Integration with Data::FormValidator and HTML::FillInForm
=back
=head2 More plug-ins
( run in 0.762 second using v1.01-cache-2.11-cpan-df04353d9ac )