CGI::Application::Dispatch is awesome! This weekend, I built a REST interface to (parts of) our application, and it was a breeze.

Let's assume you have a CGI::Application module called My::App, and that your index.cgi looks like this:

#!/usr/bin/perl use My::App; My::App->new->run;

In this situation, you would need a new instance script for every new application module. CA::Dispatch takes that pain away by giving you the means to write a single dispatch script for all your appllications.

An example for your case might be:

#!/usr/bin/perl use CGI::Application::Dispatch; CGI::Application::Dispatch->dispatch( prefix => 'My', default => 'App', );
If you save that script as index.cgi, your URL could become http://www.mydomain.com/index.cgi/loadPage?data=10.

By using more of CA::Dispatch's power, you can take that even further:

#!/usr/bin/perl use CGI::Application::Dispatch; CGI::Application::Dispatch->dispatch( prefix => 'My', table => [ '/:rm/:data?' => { app => 'App' }, ], );
This dispatch script would be able to map the following URL: http:://www.mydomain.com/index.cgi/loadPage/10. The runmode is taken from the first part of the PATH_INFO (that's what the ":rm" indicates), and the page number is captured by the ":data?". You can access the page number in your runmode with $self->param('data').

The final step would be to get rid of the "index.cgi" part in your URL, but for that you do need to edit your web server config. I generally use mod_rewrite (as I do a lot of mappings), but a suitable <Location> section would suffice as well. The CGI::Application::Dispatch documentation has some examples for you.


In reply to Re: How do I create friendlier URLS? by rhesa
in thread How do I create friendlier URLS? by boboson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.