Search code examples
phpsymfony1

how to Configure Symfony 1.4 project for multiple domains?


We have a website that developed in symfony 1.4 framework. This website should be able to have multiple domains. Each domain has its special homepage and everything else. Actually the domain must be such a parameter for each action that according to it, the action gets the data from database and show it.

For example, we have a about us page. We save about us contents in about_us table. This table has a website_id. We keep website information in the website table. Suppose this:

website (id, title, domain)
about_us (id, content, website_id)

website contents:

(1, 'foo', 'http://www.foo.com') and (2, 'bar', 'http://www.bar.com')

about_us contents:

(1, 'some foo', 1) and (2, 'some bar', 2)

The question is, how should I configure my Symfony project, to be able to do like this? to get domain as a parameter and use that in Symfony actions?


Solution

  • You can create your own route class extending sfRoute. This route will add a 'domain' parameter to all requests:

    //apps/frontend/lib/routing/myroute.class.php
    
    class myRoute extends sfRoute
    {
    
        public function matchesUrl($url, $context = array())
        {
            // first check if it is a valid route:
            if (false === $parameters = parent::matchesUrl($url, $context))
            {
               return false;
             }
    
            $domain = $context['host'];
    
            // add the $domain parameter:
            return array_merge(array(
                'domain' => $domain
                ), $parameters);
        }
    }
    

    Routing.yml (example):

    default_module:
      class: myRoute
      url:   /:module/:action/:id
      ...
    

    In your action you get the domain with:

     $request->getParameter('domain');