a blog about code, games and life in general

King23: extracting parameters from the hostname…

I just added a small feature to King23s routing that allows you to use parts of the domain as parameters.

An example use for this would be a multi user photoalbum, where the users name is part of the subdomain, for deeplink to his photos… or a multiuser blog etc. pretty much everything where a hostname is preferred.

This is not in the current release yet, but allready available in the git repository on github.

This adds a route which will match a regular parameter aswell as a hostbased one. Now to explain a bit further, lets look at a full url:http://example.king23.net/foo
the route in the example would cause the method “index” to be called in My_View, handing one parameter, which is an array, looking like: array(‘param1’ => “foo”, ‘hparam1’ => ‘net’).. “net” you wonder? well, the parts of the host are matched in reverse order, so example.king23.net first parameter would be net, second king23 and third example.
This is mostly to ensure that if you have more parameters than host segments this gets filled in a logical way.

Now this is nice, but actually you did not want the actual domain name as paramters, just subdomains of it? if you have http://username.users.king23.net then you’d be interested in username, not in users.king23.net, right? Well, there is a simple solution for that:

<?php
$router->setBaseHost('users.king23.net');

this simple line will cause the unwanted part to be removed. The route from the first example would cause “http://myusername.users.king23.net/foo&#8221; to be translated to a call to My_View::index with one array parameter containing array(‘param1’ => “foo”, ‘hparam1’ => “myusername”).