特定操作的子域

时间:2014-12-18 19:05:41

标签: symfony routing subdomain

我有这个很大的问题......

我有一个SiteManager和一个用于管理动态子域的监听器,问题是,如何将子域重定向到控制器中的特定操作,我不能使用.htacces,因为服务器使用ngix和所有这将失去意义......

我的网站管理员:

<?php

namespace Aacp\aBundle\Site;

class SiteManager
{
    private $currentSite;

    public function getCurrentSite()
    {
        return $this->currentSite;
    }

    public function setCurrentSite($currentSite)
    {

        $this->currentSite = $currentSite;
    }
}

我的听众

<?php

namespace Aacp\aBundle\EventListener;

use Aacp\aBundle\Site\SiteManager;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class CurrentSiteListener
{
    private $siteManager;
    private $em;
    private $baseHost;

    public function __construct(SiteManager $siteManager, EntityManager $em,
                                $baseHost)
    {
        $this->siteManager = $siteManager;
        $this->em          = $em;
        $this->baseHost    = $baseHost;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();

        $currentHost = $request->getHttpHost();
        $subdomain   = str_replace('.'.$this->baseHost, '', $currentHost);

        //redirect NO-www to wwww
        if($subdomain==$this->baseHost){
            header("Location: http://www.".$this->baseHost);
            exit;
        }

        //Looking subdomains from categories
        $site = $this->em
            ->getRepository('AacpaBundle:Category')
            ->findOneBy(array('subdomain' => $subdomain))
        ;

        //if no found subdomain make execption
        if(!$site){
            throw new NotFoundHttpException(sprintf(
                'No site for host "%s", subdomain "%s"', $this->baseHost,
                $subdomain
            ));

        }

        $this->siteManager->setCurrentSite($site);
    }
}
好吧,这一切都很好,但问题是总是去家庭行动,我有3个子域名www,sub1,sub2,所以www go homeAction(),sub1 go sub1Action(),sub2 go sub2Action()

的routing.yml

home:
    path:     /
    defaults: { _controller: AacpEncoderBundle:Default:home }
    host: www.%base_host% #static

sb1:
    path:   /
    defaults:   { _controller: AacpaBundle:Default:sub1 }
    host: sub1.%base_host% #static



sb2:
    path:   /
    defaults:   { _controller: AacpaBundle:Default:sub2 }
    host: sub2.%base_host% #static

所以,我需要使用drop rhis line for make dynamyc并从类别实体中获取子域

请帮助!!!我是crayz whit ... :(

0 个答案:

没有答案