in reply to How do I create friendlier URLS?

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.

Replies are listed 'Best First'.
Re^2: How do I create friendlier URLS?
by Raster Burn (Beadle) on Feb 22, 2007 at 16:07 UTC

    By the true definition of REST, that's not a pure REST interface. One of the principles of REST is to not use any verbs in your resource name because the HTTP request is the verb.

    To make your app truly RESTful, use page/10 instead of loadPage/10. An HTTP GET request should signify "load", HTTP DELETE for "delete", and so forth.

    There is some dissonance on whether POST or PUT should be used for insert or update, but the idea is that the HTTP request provides the "verb", not the resource name.

    And then, I haven't even touched on mime-types :)

    Here's some more info: Wikipedia article on REST

      You're quite right about all of that, and I have applied those principles to my REST interface at work. But I didn't say the examples for the OP were REST :-)

      CGI::Application::Dispatch has some extra features to make REST easier to implement (by attaching the HTTP verb to the runmode name, so you'd have page_GET, page_DELETE etc. as the real run modes, dispatched from the single /page url). I don't think it's relevant to the OP's question though, and would probably confuse the issue.

      By the way, I really, really like the REST approach. It encourages a clear, uncluttered implementation on the backend, and it's very simple to integrate the resulting resources into other applications. Compared to SOAP, REST is a lot cleaner ;)

      I also found the columns on xml.com helpful in gaining a more practical understanding of the REST philosophy.