punkish has asked for the wisdom of the Perl Monks concerning the following question:

I started looking for alternatives for cleaning up my web application URLs, and found CGI::Application::Dispatch. Except, I can't, for the life of me, figure out how to graft in C::A::D into my existing application.

Update: Added words in emphasis to the above para. Without them the sentence didn't make any sense.

Here is the background --

I have a reasonably mainstream application made with CGI::Application and a sprinkling of Plugins (DBH, Session, LogDispatch, AutoRunmode).

I follow the usual pattern --

package Myapp; use base 'CGI::Application'; use CGI::Application::Plugin::* sub view : StartRunmode {} sub edit : Runmode {} sub update : Runmode {} sub create : Runmode {} sub save : Runmode {} sub find : Runmode {} .. bunch of supporting subs .. sub setup {} sub cgiapp_get_query { .. require CGI::Simple; } sub cgiapp_init { }

Everything works, and now I want to clean up my URLs. Nothing fancy, just the usual stuff like so --

<server> --> <server>/index.cgi <server>/ --> <server>/index.cgi <server>/Item --> <server>/index.cgi?rm=view&id=Item <server>/(view|edit|create)/Item --> <server>/index.cgi?rm=$1&id=Item <server>/find/?q=search_term --> <server>/index.cgi?rm=find&q=search_t +erm

calls to "update" and "save" will be via POST, so they should be let in freely, and the same applies to all static content such as css, js, media, etc.

So, I started with mod_rewrite voodoo, but after 4 days of tackling it unsuccessfully, I have given up on it.

Even if I get everything working well with mod_rewrite, I am actually loathe to implement it, because in the long run, I want to do minimum amount of messing with Apache configs, something I may not have complete control over in different conditions. Keep in mind, I don't mind implement a couple of mod_rewrite rules, but doing it in my application will give me more control for future development and modification.

So, I started looking for alternatives, and found CGI::Application::Dispatch. Except, I can't, for the life of me, figure out how to graft in C::A::D into my existing application.

If I am being dense, just whack me on your head with your esc key. In any case, do guide me through this valley of confusion.

Once I figure it all out, I will write up a nice easy tutorial that even PHP folks will be able to understand. Promise.

Update: and, yes, I have looked at all the CGI::App::Dispatch articles that I could find. While they do explain stuff, they don't explain it in context of implementing CAD within and existing CGI::App application. At least, not in the way I could understand.

Update2: and, I need to do with vanilla cgi first, before I attempt all the CAD magic with mod_perl

--

when small people start casting long shadows, it is time to go to bed

Replies are listed 'Best First'.
Re: using CGI::Application::Dispatch in my existing application
by Anonymous Monk on Oct 29, 2008 at 17:28 UTC
    http://search.cpan.org/src/MARKSTOS/CGI-Application-Dispatch-2.13/t/ contains examples, anyway, try
    # index.cgi use CGI::Application::Dispatch; CGI::Application::Dispatch->dispatch( table => [ '' => { app => 'Myapp', } '/' => { app => 'Myapp', } 'Item' => { app => 'Myapp', id =>'Item', rm => 'view', } 'find' => { app => 'Myapp', id =>'Item', rm => 'find', } 'view/Item' => { app => 'Myapp', id =>'Item', rm => 'view', } 'edit/Item' => { app => 'Myapp', id =>'Item', rm => 'edit', }, 'create/Item' => { app => 'Myapp', id =>'Item', rm => 'create', }, ], ); #
      I did say I am being dense here, and that denseness continues. I saw the code you refer to. What am I to do with that code? Does it go in my package or in my index.cgi? It seems it should go in index.cgi, but putting it there (with the necessary modifications, of course) and then going to http://<server>/myapp/index.cgi/view, ends up with the error

      No such run mode 'view' at .../index.cgi

      Additionally, I can't really have id => 'Item' hard coded in my script. The string 'Item' is shown just for example. It changes all the time.

      Sorry, I am not getting it.

      --

      when small people start casting long shadows, it is time to go to bed
        Jeebus, I finally understand it!

        CAD docs need some serious rewrite (and I shall do so, soon as I figure *everything* out about it) especially for someone like me who already has a working CGI::App application and then wants to add CAD to it (and happens to be a bit dense like me).

        Great app, but docs need to be improved.

        --

        when small people start casting long shadows, it is time to go to bed