Symfony dynamic subdomains

前端 未结 4 1095
醉话见心
醉话见心 2020-12-29 10:12

I\'m trying to match subdomains to a customer id in symfony.

i.e. i have customer1.example.com and customer2.example.com

Domains are stored in a table.

4条回答
  •  星月不相逢
    2020-12-29 11:03

    Take a look at sfDomainRoutePlugin - it does what you want. However, in its current version you don't get the Propel or DoctrineRoute functionality, which means you must manually lookup the customer based on the subdomain parameter returned from the plugin. Example:

    app/frontend/config/routing.yml

    # pick up the homepage
    homepage:
      url:          /
      class:        sfDomainRoute
      param:        { module: homepage, action: index }
      requirements:
        sf_host:    [www.example.com, example.com]
    
    # catch subdomains for customers
    customer_subdomain:
      url:          /
      class:        sfDomainRoute
      param:        { module: customer, action: index }
    

    app/frontend/modules/customer/actions.class.php

    public function executeIndex(sfWebRequest $request)
    { 
      // get the subdomain parameter
      $this->subdomain = $request->getParameter('subdomain');
      // retrieve customer (you have to create the retrieveBySubdomain method)
      $this->customer = CustomerPeer::retrieveBySubdomain($this->subdomain);
    }
    

    This is just an example, but I use a similar approach myself, and the plugin does what is advertised. Good luck.

    If you're adventurous, yuo could take a look at Chapter 2 in the "More with symfony book". This would help you understand the code in sfDomainRoutePlugin.

提交回复
热议问题