Using the Zend_Controller_Router_Route_Hostname we can make nice looking personalized URL addresses like
http://username.example.com (http://john.example.com)
Lets say we have ‘userpage’ controller with ‘view’ action and want john.example.com to route to this userpage ‘controller’ and this th ‘view’ action, setting a parameter ‘username’ to ‘john’. This is easy:
$pathRoute = new Zend_Controller_Router_Route('*');
$userpageRoute = new Zend_Controller_Router_Route_Hostname(':username.example.com',
array('controller'=>'userpage', 'action'=>'view'));
$router->addRoute('userpage', $userpageRoute->chain($pathRoute));
what is to chain the Hostname route with standart Zend_Controller_Router_Route(’*') route, so we can use parameters:
http://john.example.com/param1/val1/param2/val2
this will open the ‘userpage’ controller, ‘view’ action with the following parameters:
‘username’ => ‘john’,
‘param1′ => ‘val1′,
‘param2′ => ‘val2′
But what happens when someone types in the address bar “www.example.com”? Our hostname route will match and ‘userpage’ controller with ‘view’ action will be opened with ‘username’ set to ‘www’. Most of the times this is not what we want. So we want to exclude ‘www’ subdomain somehow. Furtunately the Zend_Controller_Router_Route_Hostname constructor have third parameter, $reqs, which we can use to set requirements for the variables of the route. We have one variable - ‘username’, and want it not to match the literal ‘www’ value. So we have to write regular expression with the meaning “do not match ‘www’”. This is not so common task, and to accomplish it we will use “negative lookahead”:
^(?!www$).*$
what this means is: after the start of the string, make sure the literal ‘www$’ (www, followed by end of string) cannot be matched, then match anything else.
The match method of Zend_Controller_Router_Route_Hostname class adds the start of string and end of string anchors (^ and $), so we write the regular expression omiting these:
$pathRoute = new Zend_Controller_Router_Route('*');
$userpageRoute = new Zend_Controller_Router_Route_Hostname(':username.example.com',
array('controller'=>'userpage', 'action'=>'view'),
array('username'=>'(?!www$).*'));
$router->addRoute('userpage', $userpageRoute->chain($pathRoute));
and we are done













Fibleliagaw | 30-Dec-08 at 6:45 pm | Permalink
xdppjihqsfhwrtghwell, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch
Gonzalo | 02-Feb-09 at 3:50 pm | Permalink
Hi!.. nice tutorial, i had been trying to use the hostname router with a language i have constructed. But i find a problem, when i try to get the parameter in the subdomain, the request send me a (1), false parameter. I don`t know if this is an error of the component or i`m doing something wrong, i would like to share some code with you if you don`t mind! tell me then if you can help me in this. Anyway.. thank you form the tutorials and please… forgive me for my bad english:P
Gonzalo | 02-Feb-09 at 3:54 pm | Permalink
Hi!.. nice tutorial, i had been trying to use the hostname router with a language plugin i have constructed. But i find a problem, when i try to get the parameter in the subdomain, the request send me a (1), false parameter. I don`t know if this is an error of the component or i`m doing something wrong, i would like to share some code with you if you don`t mind! tell me then if you can help me in this. Anyway.. thank you form the tutorials and please⦠forgive me for my bad english:P
viperx | 03-Feb-09 at 6:26 pm | Permalink
Hi Gonzalo. I can’t understand your question very well. Plz post some code snippets of what you are trying to do? Post here or send to me at
viperx [at] andreinikolov.com
oxid | 24-Mar-09 at 2:44 am | Permalink
Hi! Really nice guides, just wondering if you have an exempel how it would look like if you use .ini files instead. Thanks