Oriol Planas
Oriol Planas

Reputation: 107

Change parameters.yml (database) depending on the domain used

I'd like to change som parapeters from parameters.yml depending on the domain that visitor is using to visit me.

In fact, I only need to change the database. If a user comes throw www.domain1.com I need to use the database1 but if he comes throw www.domain2.com I need to use the same code but using database2.

Is it possible?

Upvotes: 0

Views: 662

Answers (3)

Krzysztof Konopacki
Krzysztof Konopacki

Reputation: 1

In Symfony 3.4 I made it in AppKernel.php

I read domain/subdomain from $_Server['HTTP_HOST'] and then

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

    if($subdomain && file_exists($this->getRootDir().'/config/database_'.$subdomain.'.yml')){
        $loader->load($this->getRootDir().'/config/database_'.$subdomain.'.yml');
    } elseif($subdomain or ($this->getEnvironment() == 'prod' or $this->getEnvironment() == 'dev')) { //php_sapi_name() == "cli"
        $loader->load($this->getRootDir().'/config/database_default.yml');
    }

In config directory, I have files database_domain1.yml, database_domain2.yml etc.

Upvotes: 0

Mame Medoune Diop
Mame Medoune Diop

Reputation: 322

What are you trying to achieve is a multi-tenant application.

In your config file, you should create two connections for the database, one for master database which contain you application shared data and the other one which is dynamic and is for tenant specific data.

doctrine:
dbal:
    default_connection:       default
    connections:
        default:
            driver:   pdo_mysql
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
        dynamic_conn:
            driver:   pdo_mysql
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   ~
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
    # if using pdo_sqlite as your database driver:
    #   1. add the path in parameters.yml
    #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
    #   2. Uncomment database_path in parameters.yml.dist
    #   3. Uncomment next line:
    #     path:     "%database_path%"

orm:
    default_entity_manager:   default
    auto_generate_proxy_classes: "%kernel.debug%"
    entity_managers:
        default:
            connection:       default
            mappings:
                MyBundle: ~
        dynamic_em:
            connection:       dynamic_conn
            mappings:
                MyBundle: ~

And now you have to set a service for switching database dynamically by listening on request and check the domain,

I suggest you to check this repository, it's an example of what you are trying to achieve https://github.com/uirapuru/multidb.

Upvotes: 0

Juan I. Morales Pestana
Juan I. Morales Pestana

Reputation: 1147

There you have your answer. Just configure another kernel with other parameters like domain. But symfony says:

Creating aplications with multiple kernels is no longer recommended by Symfony. Consider creating multiple small applications instead.

Read this doc

after that you just need to load in the loader function the files you need taking in account that domain parameter.

Hope it helps

Edited

You also could read this another doc and create a new environment. Think this is better for you.

Hope it helps!

Upvotes: 0

Related Questions