title: Multiple symfony3 localized sites in different geographic TLD domains, sharing the same core
body: <h1>For the impatients</h1>
  
  <p>What should be the <code>routing.yml</code> strategy in a project that will deploy several locales in several domains, some of the locales being "similar", and for which the pages, logic and layouts are absolutely identical?</p>
  
  <h1>Context</h1>
  
  <p>I have an agency travel. Let's name the public web "catalog". I'm currently having the catalog in Spanish (<code>es_ES</code>) in my <code>.es</code> domain with a Symfony 3 project.</p>
  
  <p>I'll translate it into catalan (<code>ca_ES</code>) into my <code>.cat</code> domain. Next translations are going to be in English UK (<code>en_GB</code>) and Spanish Mexico (<code>es_MX</code>). In the next future, more "spanishes" and more "englishes" will be added, among others.</p>
  
  <p>I currently have 5 main routes in my symfony (there are more, but for the question here, it is enough):</p>
  
  <pre><code>/                       # proposal of value, main message
  /viajes                 # index of the catalog
  /quienes-somos          # Who we are
  /{destination}          # for example "/tailandia")
  /{destination}/{trip}   # for example "/tailandia/circuito-norte-sur")
  </code></pre>
  
  <p>Note that <code>/viajes</code> and <code>quienes-somos</code> are placed before <code>/{destination}</code> for the matcher to catch them properly.</p>
  
  <p>I plan to add about 20 languages or language-variations in the next year.</p>
  
  <h1>Deploy strategy</h1>
  
  <p>I'm currently having a deployer. The project consists of about 10 <code>.git</code> repos, one for the admin panel, one for the catalog, one for the DDD model, one for the statics, etc.</p>
  
  <h1>Domain strategy</h1>
  
  <ul>
  <li>When a regional TLD domain is available, my plan is to get them:
  
  <ul>
  <li><a href="http://example.es/" rel="nofollow noreferrer">http://example.es/</a></li>
  <li><a href="http://example.cat/" rel="nofollow noreferrer">http://example.cat/</a></li>
  <li><a href="http://example.mx/" rel="nofollow noreferrer">http://example.mx/</a></li>
  <li><a href="http://example.co.uk/" rel="nofollow noreferrer">http://example.co.uk/</a> etc.</li>
  </ul></li>
  <li>When not available, I plan to make subdomains or a directory inside a special global domain:
  
  <ul>
  <li><a href="http://it.globalexample.com/" rel="nofollow noreferrer">http://it.globalexample.com/</a></li>
  <li><a href="http://jp.globalexample.com/" rel="nofollow noreferrer">http://jp.globalexample.com/</a></li>
  </ul></li>
  <li>or alternatively:
  
  <ul>
  <li><a href="http://globalexample.com/it/" rel="nofollow noreferrer">http://globalexample.com/it/</a></li>
  <li><a href="http://globalexample.com/jp/" rel="nofollow noreferrer">http://globalexample.com/jp/</a></li>
  </ul></li>
  </ul>
  
  <h1>Static routes problems</h1>
  
  <p>I forecast spaghetti problems with the routing if I only use a single <code>.git</code> and a single <code>routing.yml</code>.</p>
  
  <p>I don't see having just:</p>
  
  <pre><code>/qui-som        # For Catalan
  /chi-siamo      # For Italian
  /quienes-somos  # HEY!! WHAT HERE? es_ES or es_MX???
  /who-we-are     # the same =&gt; en_US or en_GB?
  </code></pre>
  
  <p>They need "host" qualification.</p>
  
  <h1>Dynamic routes problems</h1>
  
  <p>Also, when the route matches <code>/{destination}</code> or <code>/{destination}/{trip}</code> I want to explore only a subset of the destinations:</p>
  
  <p>If I'm in <code>example.mx</code> and I get <code>/tailandia/circuito-sur-norte</code> I don't need to explore the full destination list in all the languages, and all the trips; only the Spanish <code>es_MX</code> seo-friendly destinations and trips.</p>
  
  <p>Similarly, if I'm in <code>example.co.uk</code> and I get <code>/thailand/circuit-south-north</code> I need only to explore the <code>en_GB</code> seo-titles to get what to render.</p>
  
  <h1>Strategies thought up to now</h1>
  
  <p>I have thought 2 strategies that could work:</p>
  
  <h2>a) Routing YML strategy</h2>
  
  <p>In the <code>routing.yml</code> have some kind of "rule" related to the "host" and set the "_locale" param by hand, something similar to:</p>
  
  <pre><code>whoWeAre_es_ES:
      path: /quienes-somos
      methods: [ GET ]
      host: example.es
      defaults:
          _controller: CatalogBundle:Legal:whoWeAre
          _locale: es_ES
  
  whoWeAre_es_MX:
      path: /quienes-somos
      methods: [ GET ]
      host: example.mx
      defaults:
          _controller: CatalogBundle:Legal:whoWeAre
          _locale: es_MX
  </code></pre>
  
  <p>NOTE/DOUBT: I don't know if that "host" thing there would actually affect the route matcher or it is only for building routes.</p>
  
  <p>In addition, this creates a problem when generating the routes. If in the home page I'm placing a link to the <code>whoWeAre</code> I should be knowing what of those routes to render and yields me in some kind of spaghetti code which is undesired.</p>
  
  <h2>b) Deployer strategy</h2>
  
  <p>Have multiple routing.yml files and tune the inclusion in the deployer:</p>
  
  <pre><code>src/CatalogBundle/Resources/config/routing_es_ES.yml
  src/CatalogBundle/Resources/config/routing_es_MX.yml
  src/CatalogBundle/Resources/config/routing_en_GB.yml
  src/CatalogBundle/Resources/config/routing_en_US.yml
  </code></pre>
  
  <p>Then in the deployer, use a <code>sed</code> over the file</p>
  
  <pre><code>app/config/routing.yml
  </code></pre>
  
  <p>and include one or other of the routing files in function of the domain I am currently deploying.</p>
  
  <p>I like this approach as each domain will have it's own "auto-contained" routing definition; and it also allows me to set the locale for the full site as a setting in the <code>config.yml</code> like this:</p>
  
  <pre><code>parameters:
      locale: en_GB
  </code></pre>
  
  <p>Instead, this creates ugly duplication. For example, all the general matching routes like <code>/{destination}/{trip}</code> will be duplicated in all the routing files.</p>
  
  <p>Okey, I could extract them common factor to aother file which is included by all of them.</p>
  
  <h2>Common problems to both approaches</h2>
  
  <p>Still the static routes only differentiate in setting the <code>_locale</code> thing but share the <code>_controller</code> part.</p>
  
  <p>I don't know if it is possible for a route in symfony to add a <code>default</code> parameter and then forward the matcher to another named route, like for example:</p>
  
  <pre><code>whoWeAre_es_ES:
      path: /quienes-somos
      host: example.es
      defaults:
          _locale: es_ES
      someKindOfReroutingThingPointingTo: whoWeAre
  
  whoWeAre_es_MX:
      path: /quienes-somos
      host: example.mx
      defaults:
          _locale: es_MX
      someKindOfReroutingThingPointingTo: whoWeAre
  
  whoWeAre_en_GB:
      path: /who-we-are
      host: example.co.uk
      defaults:
          _locale: es_MX
      someKindOfReroutingThingPointingTo: whoWeAre
  
  whoWeAre:
      methods: [ GET ]
      defaults:
          _controller: CatalogBundle:Legal:whoWeAre
  </code></pre>
  
  <h1>So... Questions</h1>
  
  <ul>
  <li>Is any of those two solutions something that I should go for?</li>
  <li>Anyone has tried any of those and has a list of objective pros-cons?</li>
  <li>What's the best <strong>documented</strong> best-practice routing strategy for multi-domain localization of a SF3 application?</li>
  <li>Is it possible for a routing definition to add some parameters and then switch to another routing rule that has common things?</li>
  </ul>
  
  <p>Thnkx!</p>