Different parameters.yml for each request in Symfony?

140 views Asked by At

Is it possible to modify parameters.yml for each request based on domain?

I want to use one application for a few websites what differ only in database connection and ext.

There are way to use something like parameters.php where are i can get app configuration based on domain and other request parameters?

How to be with Symfony's cache?

1

There are 1 answers

4
ilyes kooli On

If you are using doctrine bundle, this may help you.

You should first define both connections in parameters.yml

database.com_driver:    'pdo_mysql'
database.com_host:      'com'
database.com_port:      'port'
database.com_name:      'com'
database.com_user:      'com'
database.com_password:  'com'

database.net_driver:    'pdo_mysql'
database.net_host:      'net'
database.net_port:      'port'
database.net_name:      'net'
database.net_user:      'net'
database.net_password:  'net'

Then you define 2 dbal connections and 2 orm entity managers in your config.yml:

doctrine:
    dbal:
        connections:
            com:
                driver:   %database.com_driver%
                host:     %database.com_host%
                port:     %database.com_port%
                dbname:   %database.com_name%
                user:     %database.com_user%
                password: %database.com_password%

            net:
                driver:   %database.net_driver%
                host:     %database.net_host%
                port:     %database.net_port%
                dbname:   %database.net_name%
                user:     %database.net_user%
                password: %database.net_password%
    orm:
            com:
                connection: com
            net:
                connection: net

Then you need to pass the domain to your controllers, services.. and load the correct entity manager.

An example using a controller:

class DefaultController extends Controller
{
    public function listAction($domain) {
        switch ($domain) {
            case 'com':
            case 'net':
                break;
            default:
                //Handle error
        }
        $doctrine = $this->container->get('doctrine');
        /** @var \Doctrine\ORM\EntityManager $entityManager */
        $entityManager = $doctrine->getManager($domain);
    }
}