Symfony2 基于子域设置全局参数
Symfony2 set global parameter based on subdomain
使用 symfony 2.8,我正在使用子域,我想根据子域显示不同的(比方说)主页,我将子域存储在 Domain
table 中名为 subdomain
的列。理想情况下,当用户访问 sub.example.com
我想在数据库中搜索 'sub' 并获取 row
的 id
并将其设置为 [=19= 的全局参数]specific 域,以便我可以加载网站设置并从数据库加载其他动态数据(使用 domain_id 作为键)
这是我认为是正确的,如果有更好的方法来处理同样的问题,请告诉我,如果它对我来说是新的,我可能会得到一个朋友给予赏金。
我建议您收听 kernel.controller 活动。确保你的监听器是容器感知的,这样你就可以通过 $this->container->setParameter('subdomain', $subdomain);
来设置参数
此时您只需要检查您在适合您的地方设置的参数,例如在您的控制器操作中,这样您就可以 return,例如,根据当前子域的不同视图。
参考:
看看我的实现,使用 YAML 配置而不是数据库:https://github.com/fourlabsldn/HostsBundle。你或许能得到一些启发。
<?php
namespace FourLabs\HostsBundle\Service;
use Symfony\Component\HttpFoundation\RequestStack;
use FourLabs\HostsBundle\Model\DomainRepository;
use FourLabs\HostsBundle\Exception\NotConfiguredException;
abstract class AbstractProvider
{
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var DomainRepository
*/
protected $domainRepository;
/**
* @var boolean
*/
protected $requestActive;
public function __construct(RequestStack $requestStack, DomainRepository $domainRepository, $requestActive)
{
$this->requestStack = $requestStack;
$this->domainRepository = $domainRepository;
$this->requestActive = $requestActive;
}
protected function getDomainConfig()
{
$request = $this->requestStack->getCurrentRequest();
if(is_null($request) || !$this->requestActive) {
return;
}
$host = parse_url($request->getUri())['host'];
if(!($domain = $this->domainRepository->findByHost($host))) {
throw new NotConfiguredException('Domain configuration for '.$host.' missing');
}
return $domain;
}
}
和听众
<?php
namespace FourLabs\HostsBundle\EventListener;
use FourLabs\HostsBundle\Service\LocaleProvider;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class LocaleListener
{
/**
* @var LocaleProvider
*/
private $localeProvider;
public function __construct(LocaleProvider $localeProvider) {
$this->localeProvider = $localeProvider;
}
public function onKernelRequest(GetResponseEvent $event) {
if(HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$event->getRequest()->setLocale($this->localeProvider->getLocale());
}
}
使用 symfony 2.8,我正在使用子域,我想根据子域显示不同的(比方说)主页,我将子域存储在 Domain
table 中名为 subdomain
的列。理想情况下,当用户访问 sub.example.com
我想在数据库中搜索 'sub' 并获取 row
的 id
并将其设置为 [=19= 的全局参数]specific 域,以便我可以加载网站设置并从数据库加载其他动态数据(使用 domain_id 作为键)
这是我认为是正确的,如果有更好的方法来处理同样的问题,请告诉我,如果它对我来说是新的,我可能会得到一个朋友给予赏金。
我建议您收听 kernel.controller 活动。确保你的监听器是容器感知的,这样你就可以通过 $this->container->setParameter('subdomain', $subdomain);
此时您只需要检查您在适合您的地方设置的参数,例如在您的控制器操作中,这样您就可以 return,例如,根据当前子域的不同视图。
参考:
看看我的实现,使用 YAML 配置而不是数据库:https://github.com/fourlabsldn/HostsBundle。你或许能得到一些启发。
<?php
namespace FourLabs\HostsBundle\Service;
use Symfony\Component\HttpFoundation\RequestStack;
use FourLabs\HostsBundle\Model\DomainRepository;
use FourLabs\HostsBundle\Exception\NotConfiguredException;
abstract class AbstractProvider
{
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var DomainRepository
*/
protected $domainRepository;
/**
* @var boolean
*/
protected $requestActive;
public function __construct(RequestStack $requestStack, DomainRepository $domainRepository, $requestActive)
{
$this->requestStack = $requestStack;
$this->domainRepository = $domainRepository;
$this->requestActive = $requestActive;
}
protected function getDomainConfig()
{
$request = $this->requestStack->getCurrentRequest();
if(is_null($request) || !$this->requestActive) {
return;
}
$host = parse_url($request->getUri())['host'];
if(!($domain = $this->domainRepository->findByHost($host))) {
throw new NotConfiguredException('Domain configuration for '.$host.' missing');
}
return $domain;
}
}
和听众
<?php
namespace FourLabs\HostsBundle\EventListener;
use FourLabs\HostsBundle\Service\LocaleProvider;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class LocaleListener
{
/**
* @var LocaleProvider
*/
private $localeProvider;
public function __construct(LocaleProvider $localeProvider) {
$this->localeProvider = $localeProvider;
}
public function onKernelRequest(GetResponseEvent $event) {
if(HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$event->getRequest()->setLocale($this->localeProvider->getLocale());
}
}