NAME CGI::Easy::URLconf - map url path to handler sub and vice versa VERSION This document describes CGI::Easy::URLconf version v2.0.1 SYNOPSIS use CGI::Easy::URLconf qw( setup_path path2view set_param ); setup_path( '/about/' => \&myabout, '/terms.php' => \&terms, qr{\A /articles/ \z}xms => \&list_all_articles, ); setup_path( qr{\A /articles/(\d+)/ \z}xms => set_param('year') => \&list_articles, qr{\A /articles/tag/(\w+)/(\d+)/ \z}xms => set_param('tag','year') => \&list_articles, ); setup_path( POST => '/articles/' => \&add_article, ); my $r = CGI::Easy::Request->new(); my $handler = path2view($r); use CGI::Easy::URLconf qw( setup_view view2path with_params ); setup_view( \&list_all_articles => '/articles/', \&list_articles => [ with_params('tag','year') => '/articles/tag/?/?/', with_params('year') => '/articles/?/', ], ); # set $url to '/about/' my $url = view2path( \&myabout ); # set $url to '/articles/' my $url = view2path( \&list_all_articles ); # set $url to '/articles/2010/?month=12' my $url = view2path( \&list_articles, year=>2010, month=>12 ); DESCRIPTION This module provide support for clean, user-friendly URLs. This can be archived by configuring web server to run your CGI/FastCGI script for any url requested by user, and let you manually dispatch different urls to corresponding handlers (subroutines). Additionally, you can take some CGI parameters from url's path instead of usual GET parameters. The idea is to set rules when CGI/FastCGI starts using: a) setup_path() - to map url's path to handler subroutine (also called "view") b) setup_view() - to map handler subroutine to url and then use: a) path2view() - to get handler subroutine matching current url's path b) view2path() - to get url matching some handler subroutine (for inserting into HTML templates or sending redirects). Example: # -- while CGI/FastCGI initialization setup_path( '/articles/' => \&list_articles, '/articles.php' => \&list_articles, '/index.php' => \&show_home_page, ); setup_path( POST => '/articles/' => \&add_new_article, ); # -- when beginning to handle new CGI/FastCGI request my $r = CGI::Easy::Request->new(); my $handler = path2view($r); # $handler now set to: # \&list_articles if url path /articles/ and request method is GET # \&add_new_article if url path /articles/ and request method is POST # \&list_articles if url path /articles.php (any request method) # \&show_home_page if url path /index.php (any request method) # undef (in all other cases) # -- while CGI/FastCGI initialization setup_view( \&list_articles => '/articles/', # we don't have to configure mapping for \&show_home_page # and \&add_new_article because their mappings can be # unambiguously automatically detected from above setup_path() ); # -- when preparing reply (HTML escaping omitted for simplicity) printf 'Articles', view2path(\&list_articles); printf '