Skip to content

Instantly share code, notes, and snippets.

@joegreen88
Created November 24, 2012 16:03
Show Gist options
  • Save joegreen88/4140295 to your computer and use it in GitHub Desktop.
Save joegreen88/4140295 to your computer and use it in GitHub Desktop.
Route static subdomains to controllers in zend framework
if (PHP_SAPI != 'cli') //subdomain routing
{
$domainname = $this->getOption('domainname');
$subdomains = $this->getOption('subdomains');
$router = $this->bootstrap('frontController')->getResource('frontController')->getRouter();
// routes for mainsite (default router is added)
$wwwRoute = new Zend_Controller_Router_Route_Hostname(
$domainname,
array('module' => 'default')
);
$router->addRoute(
'default',
$wwwRoute->chain(
new Zend_Controller_Router_Route(
':controller/:action/*',
array('controller'=>'index', 'action'=>'index')
)
)
);
// routes for subdomains (xyz.domain => subxyzController)
foreach ($subdomains as $subdomain)
{
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
$subdomain.'.'.$domainname,
array('module' => 'default', 'controller'=>'sub'.$subdomain)
);
$router->addRoute(
$subdomain,
$subdomainRoute->chain(
new Zend_Controller_Router_Route(
'/:action/*',
array('action'=>'index')
)
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment