« Return to Thread: map domain name to module

Re: map domain name to module

by Ivan Shumkov :: Rate this Message:

Reply to Author | View in Thread

Michael Depetrillo wrote:
Has anyone written any code to map a domain name to a specific module? I would like to be able to have a admin-specific-domain.com parked on top of my application and then route all requests to the admin module.

Hello.

$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory(array('default' => '../controllers/',
								   'admin' => '../admin/controllers/'));

$router = new Zend_Controller_Router_Rewrite();
$router->removeDefaultRoutes();

$router->addRoute('default', new SmartShop_Controller_Router_SubdomainRoute(
  ':controller/:action/*',
  array('controller' => 'index', 'action' => 'index')
));

$frontController->setRouter($router);
$frontController->dispatch();

class SmartShop_Controller_Router_SubdomainRoute extends Zend_Controller_Router_Route
{
    function match($path)
    {
        // get subdomain
        $subdomains = explode('.', $_SERVER['HTTP_HOST']);
        $countSubdomains = count($subdomains);
        if ($countSubdomains == 2 || ($countSubdomains == 3 && $subdomains[0] == 'www')) {
            $subdomain = 'default';
        } else if ($subdomains[0] == 'www') {
            $subdomain = $subdomains[1];
        } else {
            $subdomain = $subdomains[0];
        }

        // if subdomain is listed in requirements, check for match
        if(isset($this->_requirements['module']) &&
           $subdomain != $this->_requirements['module']
        ) {
            return false; // this route failed
        }

        $params = parent::match($path);
        if($params) $params['module'] = $subdomain;

        return $params;
    }
}

 « Return to Thread: map domain name to module