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


1 Answer 1


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.

  • i would be using the universityID quite a lot, since its a multitenant application, many tables would have the universityId as the foreign key, so it would not be feasible to fetch it every time, if it were to be determined once during startup it would be more elegant and easy to maintain. also with the dynamic subdomains i would need to edit the hosts file too in order to have dynamic subdomains, so i cant avoid writing server config dynamically Jun 3, 2015 at 6:56
  • I was trying to suggest that you determine the universityID within a service, rather than at startup, but this would still only be a case of working it out once. I'm not sure I understand the need to edit your hosts file dynamically - in development, perhaps, but in production I would think a wildcard DNS entry would suffice. Anyway, the basis of my answer was intended to be that you should consider the problem of loading university specific data as part of your business logic, and not try to handle it by adding dynamism into an aspect of Symfony that is designed to be static.
    – Jez
    Jun 3, 2015 at 8:18
  • this is my first project, so i guess i'm a bit muddled about this whole symfony architecture, i tried it out your way and it works, and do you know of any learning resources online apart from symfony documentation, knpuniversity. i'm looking for intermediate level stuff, i got the basics out of the way. there seems to be no new books written on this topic :'( Jun 3, 2015 at 8:37
  • Unfortunately I'm not aware of much more documentation than that. You could try reading up on more general topics like service oriented architecture and dependency injection - those are the main ideas in Symfony. Otherwise, bear in mind that any architecture is better than no architecture - you can (usually) change things around later.
    – Jez
    Jun 3, 2015 at 8:46

Your Answer

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

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