NAME

CGI::Application::Plugin::Routes - Routes-style dispatching for CGI::Application

SYNOPSIS

CGI::Application::Plugin::Routes tries to bring to Perl some of the goodies of Rails routes by allowing the creation of a routes table that is parsed at the prerun stage against $ENV{PATH_INFO}. The result of the process (if there's any match at the end of the process) is added to CGI query method from CGI::Application and available in all the runmodes via $self->query->param. By doing this, the plugin provides a uniform way of accessing GET and POST parameters when using clean URIs.

Example:

In TestApp.pm

	package TestApp;
	use strict;
	use warnings;
	use base qw/CGI::Application/;
	use CGI::Application::Plugin::Routes;
	sub setup {
		my $self = shift;

        # routes_root optionally is used to prepend a URI part to every route
		$self->routes_root('/thismod'); 
		$self->routes([
			'' => 'home' ,
			'/view/:name/:id/:email'  => 'view',
		]);
		$self->start_mode('show');

		$self->tmpl_path('templates/');
	}
	sub view {
		my $self  = shift;
		my $q     = $self->query();
		my $name  = $q->param('name');
		my $id    = $q->param('id');
		my $email = $q->param('email');
		my $debug = $self->routes_dbg; #dumps all the C::A::P::Routes info
		my $params = $self->routes_params; #shows params found.
		return $self->dump_html();
	}
	1;

Note that we did not have to call run_modes() to register the run modes. CGI::Application::Plugin::Routes will automatically register each route as run modes if there is no run mode registered with that name, and your application can call target as a method.

EXPORTED METHODS

routes

Is exported so it can be called from the CGI::Application app to receive the routes table. If no routes table is provided to the module, it will warn and return 0 and no harm will be done to the CGI query params.

routes_root

This method makes it possible to set a common root for all the routes passed to the plugin, to avoid unnecessary repetition.

routes_parse

Is exported in order to make the callback available into the CGI::Application based app. Not meant to be invoked manually.

routes_params

This method return a array of all the params found in the query_string

routes_dbg

Is exported so you can see what happened on the Routes guts.

AUTHOR

Julián Porta, <julian.porta at gmail.com>

BUGS

Please report any bugs or feature requests to bug-cgi-application-plugin-routes at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application-Plugin-Routes. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc CGI::Application::Plugin::Routes

You can also look for information at:

ACKNOWLEDGEMENTS

Michael Peter's CGI::Application::Dispatch module that can be found here: http://search.cpan.org/~wonko/CGI-Application-Dispatch I borrowed from him most of the routine that parses the url.

Mark Stosberg http://search.cpan.org/~markstos/ Provided very valuable feedback and some useful patches and changes to the code.

COPYRIGHT & LICENSE

Copyright 2008 Julián Porta, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.