PHP Symfony – Multi-Tenancy Architecture with Subdomain-Based Data Loading

ArchitecturemultitenancyPHPsymfony

Hello fellow programmers,

(a bit of background)I'm building a Symfony 2 application for university Student accommodation, when our clients from the university side register with their details a subdomain is created, students belonging to a particular university can access the online accommodation service through this subdomain, there will be several universities that will be registered and each university will have their own subdomain and separate set of related-data/student-data that cannot be accessed from other universities.

(what i have already done) i managed to create a nice login and register pages and also managed to create subdomains by editing the hosts file (with file_put_contents() any better way to do this using symfony?) then i created a service that fetches the UniversityID from the parameters.yml, this way i can get university specific data since all tables have a universityID column.

should i create a script that runs before every thing is loaded that would dynamically update the UniversityID inside the parameters.yml based on the subdomain? what is the best practice for this

In conclusion, what i want from the community is,

1). i'm looking to use the Front controller to load the University Specific data, how do i load/set dynamic data using front controller and/or Kernel

2). how to create subdomains from dynamically without being platform (OS) specific

-thanks

Best Answer

It seems like you're viewing the problem as one of re-configuring your application based on the subdomain it is accessed from.

Given that your university specific data is basically user data - i.e. it will change as users sign up for and use your application, I think a better approach is to store all of that stuff in your database and then create a service for accessing it when you need to, rather than as part of the application startup.

I would write a simple and minimal service that inspects the subdomain and returns the university ID. (You'd need to inject the @request_stack into it (see https://stackoverflow.com/a/20502945/86780), access the hostname of the current request and perform some sort of lookup.)

As for creating subdomains dynamically - I'd suggest that you configure your webserver to work with any subdomain (i.e. *.yourdomain.tld) and have your application figure out whether it's valid or not.

Basically, avoid writing server config dynamically. This approach is likely give you problems when it comes to scaling and tuning your application.

Related Topic