CGI::Application::Plugin::Routes - Routes-style dispatching for CGI::Application
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.
$self->query->param
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.
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.
This method makes it possible to set a common root for all the routes passed to the plugin, to avoid unnecessary repetition.
Is exported in order to make the callback available into the CGI::Application based app. Not meant to be invoked manually.
This method return a array of all the params found in the query_string
Is exported so you can see what happened on the Routes guts.
Julián Porta, <julian.porta at gmail.com>
<julian.porta at gmail.com>
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.
bug-cgi-application-plugin-routes at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc CGI::Application::Plugin::Routes
You can also look for information at:
github
http://github.com/Porta/cgi--application--plugin--routes/tree/master
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=CGI-Application-Plugin-Routes
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/CGI-Application-Plugin-Routes
CPAN Ratings
http://cpanratings.perl.org/d/CGI-Application-Plugin-Routes
Search CPAN
http://search.cpan.org/dist/CGI-Application-Plugin-Routes
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 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.
To install CGI::Application::Plugin::Routes, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CGI::Application::Plugin::Routes
CPAN shell
perl -MCPAN -e shell install CGI::Application::Plugin::Routes
For more information on module installation, please visit the detailed CPAN module installation guide.