Symfony2:根据域加载配置文件 [英] Symfony2: Load config-files depending on domain

查看:27
本文介绍了Symfony2:根据域加载配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我们正在为德国经营一家网上商店.现在我们还希望在英国提供我们的产品,并拥有自己的域名.

At the moment we are running an online-shop for germany. Now we want also offer our products in UK with an own domain.

根据域的不同,它们有几个应该加载的设置:

Depending on the domain their are several settings which should be loaded:

  • Google Analytics ID
  • 支付 API 秘密/密钥,...
  • 货币
  • 语言
  • 管理员邮件
  • 跟踪像素 (FB)
  • 还有更多....

在之前的项目中,我们通过将此设置放在数据库中的域表中来解决此问题.但我认为对于整个支付服务信息和密钥以及和......这不是最好的解决方案.

In a previous project we solved it by putting this settings in a domain-table in the database. But I think with the whole payment service information and key and and and.. it is not the best solution.

推荐答案

你可以写一个 bundle 扩展类 根据主机加载您的配置.

You can write a bundle Extension class to load your configuration depending on the host.

捆绑扩展:

// src/AcmeBundle/DependencyInjection/AcmeExtension.php

<?php

namespace AcmeBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class AcmeExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
        $rootdir = $container->getParameter('kernel.root_dir');

        // Load the bundle's services.yml
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');

        // Load parameters depending on current host
        $paramLoader = new Loader\YamlFileLoader($container, new FileLocator($rootdir.'/config')); // Access the root config directory
        $parameters = sprintf('parameters_%s.yml', $container->getParameter('router.request_context.host'));

        if (!file_exists($rootdir.'/config/'.$parameters)) {
            $parameters = 'parameters.yml'; // Default
        }

        $paramLoader->load($parameters); 
    }
}

对应的bundle配置:

The corresponding bundle Configuration:

// src/AcmeBundle/DependencyInjection/Configuration.php

<?php

namespace AcmeBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('acme');

        return $treeBuilder;
    }
}

像这样,您可以创建一个名为 parameters_localhost.yml 的文件,它会自动加载.如果未找到该文件,则将使用默认的 parameters.yml.

Like this, you can create a file named parameters_localhost.yml and it will be automatically loaded. If the file is not found, the default parameters.yml will be used.

你可以根据你想要的每个参数应用这个逻辑(比如 _locale 用于你的翻译,我猜).

You can apply this logic based on every parameters you want (like _locale used for your translations, I guess).

希望对您有所帮助.

这篇关于Symfony2:根据域加载配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆