简体   繁体   中英

Partitioning a Symfony2 application into subdomains

I am building a symfony2 application, and I want to split the functionality into two "sites", for example:

  • www.mydomain.com (basic website)
  • foobar.mydomain.com (restricted portion of site, providing specific functionality)

[Clarification]

I have already read the documentation pointed out by others below, but Symfony is throwing an exception, with the following error message :

The parameter "domain" must be defined (500 Internal Server Error)

This is what I have done so far:

  1. added foobar.localhost to my /etc/hosts file
  2. Modified by routing.yml (see below)
  3. Restarted Apache

My routing.yml file looks like this

foobar_homepage:
    path:     /
    host:     "foobar.{domain}"
    defaults:
        _controller: AcmeDemoBundle:Main:foobarHomepage
        domain: "%domain%"
    requirements:
        domain: "%domain%"

homepage:
    path:  /
    defaults: { _controller: AcmeDemoBundle:Main:homepage }

I don't want to hardcode the domain name - so I can use the same codebase and configuration, for multiple sites.

I already have the domain name in the request object. The problem is that its's not clear how to pass that parameter to the route.

So how can I pass the domain name in a DRY fashion, in order to satisfy the route requirement?

If you want fully dynamic host parsing in route without any hardcoded value you should omit default value and requirements for your route. It should look like that:

foobar_homepage:
    path:     /
    host:     "foobar.{domain}"
    defaults:
        _controller: AcmeDemoBundle:Main:foobarHomepage

Is your problem the route matching? If yes, take a look at the symfony documentation: http://symfony.com/doc/current/components/routing/hostname_pattern.html

You can even make dynamic subdomains. This is a nice video tutorial about it: http://knpuniversity.com/screencast/question-answer-day/symfony2-dynamic-subdomains

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM