Here we go Route de Route

in principio creavit

Life was easy on the web so many years ago, urls where simple. You only bothered had one.

www.mysillylife.com/main.cgi

You simply tacked on all sort of extra little bits on end to know, what to do, what to show, where and who are you, and such things as how long you have been here. Something akin to;

www.mysillylife.com/main.cgi?user=blogins&lastvisit=1928282&mode=update&sessionkey=24823498....

anyway you get the picture. We only had get and post and a good number of sites dropped get when they discovered you could hide things in post. Of course you would see cases just post to the same page and your "mode" took care of things for you or some pages knew what to do with a post or a get.

divisit lucem ac tenebras

Thank goodness things have changed, and most of you out there will not remember these dark days of the early web. The new concepts are is quite simple, one now exposes just the routes one wants and our URLs read like what they are suppose to do (hey that is very perl like). Be fore-warned though we should still be careful on how we play out there on the web. As an aside this is an old idea that is just catching on some 14 yeas later.

Lest have a look a Mojolicious routes and a simple problem that someone had the other day.

The desired effect was

www.somesite/cities/paris/show

ok easy enough with Mojo

$route->route("cities/:controller/:show");

So we are going to the 'Paris' controller and then hitting the 'show' sub.

Ok that will work but there is a vast number of cities out there does it make sense to have one controller for each with one sub show? A little anti-pattern in the offing?

ad nostri collyridam et comedite illud

Fortunately with mojo we can do a little better

We could try

$route->route("/cities/:action") ->to("cities#show");

but you now loose you 'show' at the end so how about

$route->route("/cities/:city_id/:action");

Ok now you are playing about with your params and defaulting to the 'cities' controller with whatever "action" you have at the end.

One thing that is nice about Mojo, and many other systems of its ilk, is there are many ways to do the right thing. How about this,

my $city = "paris";
my $action = "show";

$route->route("/cities/$city/$action") ->to("cities#$city",{action=>$action});

you could if you wanted have all those 'City' controllers but stack them under a 'cities' folder like this

$route->route("/cities/$city/$action") ->to("cities-$city#$action");

and we have only scratched the surface as I haven't mentioned such funny things as PUT and DELETE requests.

E quindi uscimmo a riveder le stelle

In the end there always more than one way to do it, with flexibility of today's web frameworks comes great power but that power come great responsibility. Take the time to do a little reading on restful web services decide what resources you want and how you want to expose them.

Ask you self do you really want your web pages to follow a restful pattern?
Perhaps the web site should just be one page that hooks into 200 json web services?

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations