0

It is possible in symfony2 and fosuserbundle (or inner) to make it:

When I enter to site by host example.com I choose "example" database, then I can login to user in this database. When I enter to site by host exaple2.com I choose "exaple2" database where are other users and data.

Routing for both host I wanna have this same.

Edit:

I trying it now with MongoDB. I can create user for one datebase but can't login becouse it throw "Invalid credentials".

Config:

doctrine_mongodb:
    connections:
        pl:
            server: mongodb://localhost:27017
        en:
            server: mongodb://localhost:27017
    document_managers:
        pl:
            connection: pl
            mappings:
                AcmeBundle: ~
                FOSUserBundle: ~
        en:
            connection: en
            mappings:
                AcmeBundle: ~
                FOSUserBundle: ~

Request listener

class RequestListener
{
protected $dm;

public function setDmManager(ObjectManager $dm)
{
   $this->dm = $dm;
}
public function onKernelRequest(GetResponseEvent $event)
{
        $dbname = $event->getRequest()->getLocale();
        $this->dm->getConfiguration()->setDefaultDB($dbname);

}
}

1 Answer 1

1

Yes it is quite doable using Event Listeners like OnKernelRequest, I've got an application that does just that with MongoDB.

I name each DB according to domain & subdomain (ie: forums-mysite)

Then I use an Event Listener on OnKernelRequest to get the host name, break it down & load the appropriate database. Those lines should help:

    $request = $event->getRequest();
    $currentHost = $request->getHttpHost();

That is to get the host, then you need to parse it (with explode() or the such) to get the database name.

$this->dm->getConfiguration()->setDefaultDB($dbname);

That is to switch DB, but $this->dm is the DocumentManager for mongoDB so it could be different for SQL and the EntityManager, you'll have to look up the appropriate method.

Hope this helps a bit.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.