We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Multi domain site ?

For a Saas application I want to setup a multi-domain website, all using the same database. (like company1.site.com , company234.site.com, etc. )

How can this be done using Phalcon? (I havent been able to find any tutorial about this)



85.5k
edited Oct '15

i would do it from apache/nginx conciguration.

For exmaple: company1.ex.com will have root directory : /var/www/ex.com/public, company2.ex.com will have the same root dir as comp1.

after that in your code you can make a configuration service, inside you can have something like this :

list($this->subDomain, $this->domain) = explode('.', $_SERVER['SERVER_NAME'], 2);
.....

and after that you can keep on going.


if (di::getDedeualt()->get('config')->subdomain === 'companu1'){
    pdo connect  ( select db : company.ex.com )
}

Hopefully that helps



1.6k

yes, nginx would just use a wildcard, server_name *.example.com;

All subdomains should use the same database, because I dont want to have 1000 databases . So there should be some table where the subdomains are listed. I imagine that in public/index.php I should check the subdomain, look it up in the db and get the ID. That is what I want to use elsewhere in the application, in for example the Controllers. how do I make that available ?

All subdomains should use the same database, because I dont want to have 1000 databases .

Better have 1000 datavases - it's will be more easy for scaling

So there should be some table where the subdomains are listed. I imagine that in public/index.php I should check the subdomain, look it up in the db and get the ID. That is what I want to use elsewhere in the application, in for example the Controllers. how do I make that available ?

You can get id after crate instance of application class but before handle request in default entry point (index.php in public dir)

$app = new Application($di);

// Any code

echo $app->handle()->getContent();

or use event beforeHandleRequest https://docs.phalcon.io/en/latest/reference/applications.html#application-events