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.

A very simple usage example:

// get instace of the mainrouter
$router = King23_Router::getInstance();
$router->addRoute("/", "My_View", "index", array('param1'), array('hparam1'));

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:
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 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 then you’d be interested in username, not in, right? Well, there is a simple solution for that:


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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s