Symfony2: loading configuration files depending on the domain

At the moment we have an online store for Germany. Now we also want to offer our products in the UK with our own domain.

Depending on the domain, they have several parameters that need to be loaded:

  • Analytics Id
  • Secrets / API keys for payments, ...
  • Currency
  • Tongue
  • Administrative mail
  • Pixel Tracking (FB)
  • and much more....

In the previous project, we solved it by placing these settings in the domain table in the database. But I think that all information about payment services is the key and ... and this is not the best solution.

+4
source share
4 answers

.

:

// 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 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); 
    }
}

:

// 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.

, (, _locale, ).

.

+5

, parameters.yml? ParameterHandler script, .

, , , , , , DE ..? ( )

, - , , . , .

, DE ..

, , , ..

Sylius GitHub. ChannelBundle, , .

+2

SetEnv (apache?) (ref)

# /etc/apache2/sites-available/one-domain.conf
<VirtualHost *:80>
    ServerName      onedomain.uk
    DocumentRoot    "/path/to/onedomain/web"
    DirectoryIndex  index.php index.html
    SetEnv          SYMFONY__GOOGLE__ANALYTICSID mygoogleanalyticsid
    SetEnv          SYMFONY__PAYMENT__APISECRET mysecret
    # ...
    <Directory "/path/to/onedomain/web">
        AllowOverride All
        Allow from All
    </Directory>
</VirtualHost>

.yml, :

# app/config/parameters.yml
parameters:
    analytics_id: "google.analyticsid"
    payment_secret: "payment.apisecret"
    # ...

db , .

, , (ref):

# app/AppKernel.php
    public function getCacheDir()
    {
        if(getenv("SYMFONY__DOMAIN__NAME")){
            return dirname(__DIR__).'/var/cache/'.$this->getEnvironment().'/'.getenv("SYMFONY__DOMAIN__NAME");
        } else {
            return dirname(__DIR__).'/var/cache/'.$this->getEnvironment();
        }
    }

    public function getLogDir()
    {
        if(getenv("SYMFONY__DOMAIN__NAME")){
            return dirname(__DIR__).'/var/logs/'.getenv("SYMFONY__DOMAIN__NAME");
        } else {
            return dirname(__DIR__).'/var/logs/';
        }
    }

If you store sessions in files, you can also do this for the sessions folder:

# app/config/config.yml
framework:
    # ...
    session:
        handler_id:  session.handler.native_file
        save_path:   "%kernel.root_dir%/../var/sessions/%kernel.environment%/%domain.name%"
0
source

I know that the best answer has already been chosen, but I found a different approach with parameters.php:

<?php

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader;

$hostArray = explode('.', $_SERVER['HTTP_HOST']);
$hostArrayCount = count($hostArray);
$host = $hostArray[$hostArrayCount - 2] . '.' . $hostArray[$hostArrayCount - 1];
$hostDir = $container->getParameter('kernel.root_dir') . '/config/domains/';
$parameters = sprintf('%s.yml', $host);

if (file_exists($hostDir . $parameters)) {
    $hostParamLoader = new Loader\YamlFileLoader($container, new FileLocator($hostDir));
    $hostParamLoader->load($parameters);
}

and config.ymlafter:

imports:
    - { resource: parameters.yml }

add line:

    - { resource: parameters.php }
0
source

All Articles