Skip to content

Instantly share code, notes, and snippets.

@tfountain
Created July 31, 2014 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tfountain/bbf4a6cd2b3681570ab3 to your computer and use it in GitHub Desktop.
Save tfountain/bbf4a6cd2b3681570ab3 to your computer and use it in GitHub Desktop.
ZF2 multi-tenant
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
// load site
$eventManager->attach(MvcEvent::EVENT_ROUTE, function ($e) {
$serviceManager = $e->getApplication()->getServiceManager();
$request = $e->getRequest();
$siteMapper = $serviceManager->get('Application\Site\SiteMapper');
$query = $siteMapper->buildQuery();
$query->where(array('domain' => $request->getServer('HTTP_HOST')));
$site = $siteMapper->fetchObject($query);
if (!$site) {
// TODO
}
// store the site in the service manager
$serviceManager->setService('Site', $site);
});
// if a site was found, add its theme path
$eventManager->attach(MvcEvent::EVENT_RENDER, function($e) {
$serviceManager = $e->getApplication()->getServiceManager();
$templatePathResolver = $serviceManager->get('Zend\View\Resolver\TemplatePathStack');
$site = $serviceManager->get('Site');
if ($site && $site->template_location) {
$templatePathResolver->addPath('./themes/'.$site->template_location.'/view');
}
}, 100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment