Symfony:有可能加载不同的参数. [英] Symfony: It's possible load different parameters.yml?

查看:68
本文介绍了Symfony:有可能加载不同的参数.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在symfony上加载不同的parameter.yml吗?我有一个SaaS多租户APP,希望在每个租户的基础上加载differents parameter.yml(带有配置数据库),以便在登录操作时将用户分派到正确的数据库.

It's possible to load different parameters.yml on symfony? I have a SaaS multitenant APP and I wish load differents parameters.yml (with config DB) base on each tenant in order to dispatch the user to the correct DB on login action.

非常感谢!

已解决!

我解决了在文件夹中安排不同租户配置的问题,

I solved it arranging the different tenants configurations in folders like:

app
---config
------tenantA
---------config.yml
---------config_dev.yml
---------config_prod.yml
---------parameters.yml
------tenantB
---------config.yml
---------config_dev.yml
---------config_prod.yml
---------parameters.yml
------tenantC
---------config.yml
---------config_dev.yml
---------config_prod.yml
---------parameters.yml
....

然后,修改kernelApp.php的加载程序:

And, modifying the loader of kernelApp.php:

$ loader-> load($ this-> getRootDir().'/config/租户/config _'.$ this-> getEnvironment().'.yml');

$loader->load($this->getRootDir().'/config/TENANT/config_'.$this->getEnvironment().'.yml');

我不知道这是否是个好习惯,但是效果很好!

I don't know if this is a good practice but it works fine!

推荐答案

您在这里有几种可能.

如果租户号是一个常数或接近它(很少增加新的租户),则可以按照

If tenant's number is a constant or close to it (new tenants are added very rarely) you can simply setup several connections as described here. After that you can get necessary EntityManager instance by calling

$em = $this->get('doctrine')->getManager($dynamicValue);

此方法的缺点是,每次需要创建/更新/删除租户时,都必须修改config.yml.

Downside of this approach is that you must modify your config.yml each time you need create/update/delete a tenant.

但是,如果您想要更灵活的方法,则可以实现自己的类来管理数据库连接:

But if you want more flexible approach you can implement your own class for managing database connections:

<?php

namespace AppBundle\Service;

use AppBundle\Entity\Database;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;

/**
 * Class ConnectionManager
 * @package AppBundle\Service
 */
class ConnectionManager
{
    /**
     * @var Connection[]
     */
    protected $connections = [];

   /**
     * @param Database $database
     * @return Connection
     * @throws DBALException
     */
    public function getConnection(Database $database)
    {
        $params = [
            'dbname' => $database->getName(),
            'user' => $database->getUser(),
            'password' => $database->getPassword(),
            'host' => $database->getHost(),
            'driver' => 'pdo_mysql',
        ];

        $key = $this->getKey($params);
        if (isset($this->connections[$key])) {
            return $this->connections[$key];
        }

        $conn = DriverManager::getConnection($params);
        $this->connections[$key] = $conn;
        return $conn;
    }

    /**
     * @param array $params
     * @return string
     */
    protected function getKey($params)
    {
        sort($params);
        return md5(implode('.', $params));
    }
}

这篇关于Symfony:有可能加载不同的参数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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