我已经关注了knpUniversity"如何处理symfony2中的动态子域" .Routing子域工作,但当我尝试去www.domain.com我得到错误,如果没有找到子域我抛出。如何解决这个问题? 这是我的代码:
public function onKernelRequest(GetResponseEvent $getResponseEvent)
{
$request = $getResponseEvent->getRequest();
$host = $request->getHost();
$base_host = $this->baseHost;
$sub_domain = str_replace('.'.$base_host,'',$host);
$site = $this->em->getRepository('AppBundle:Client')->findOneBy(['subDomain' => $sub_domain]);
if(!$site){
throw new NotFoundHttpException(sprintf(
'Cannot find site for host "%s", subdomain "%s" ',
$host,
$sub_domain
));
}
$siteManager = $this->siteManager;
$siteManager->setCurrentSite($site);
}
答案 0 :(得分:1)
我不知道这是否是最好的方法,但我已经成功了。
public function onKernelRequest(GetResponseEvent $getResponseEvent)
{
$request = $getResponseEvent->getRequest();
$host = $request->getHost();
$base_host = $this->baseHost;
$sub_domain = str_replace('.'.$base_host,'',$host);
$site = $this->em->getRepository('AppBundle:Client')->findOneBy(['subDomain' => $sub_domain]);
if(!$site && $base_host != $sub_domain){
throw new NotFoundHttpException(sprintf(
'Cannot find site for host "%s", subdomain "%s" ',
$host,
$sub_domain
));
}
$siteManager = $this->siteManager;
$siteManager->setCurrentSite($site);
}