Note that there are some explanatory texts on larger screens.

plurals
  1. PODetect language and redirect to subdomain on Symfony
    primarykey
    data
    text
    <p>I'm using Symfony 1.2.7. My web is in several languages, each of one is in a subdomain like en.example.com, es.example.com. If the user enters into example.com, I want to redirect him to his language. I also want to have support staging.example.com and redirect to es.staging.example.com and en.staging.example.com so I can test everything before the deployment.</p> <p>I have the following code that works both on index.php and frontend_dev.php. My question is, can you improve it? is there a better or cleaner way? Thanks!</p> <pre><code>require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php'); $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'dev', true); $context = sfContext::createInstance($configuration); // get the domain parts as an array $host = array_reverse(explode('.', $_SERVER['HTTP_HOST'])); list($tld, $domain, $subdomain) = $host; // determine which subdomain we're looking at $app = ($subdomain == 'staging') ? $subdomain2=$host[3] : $subdomain; if(empty($app) || $app == 'www') { $browser_languages = $context-&gt;getRequest()-&gt;getLanguages(); foreach($browser_languages as $language) { $allowed_culture = in_array($language, sfConfig::get('app_languagesAvailable')); if($allowed_culture) { $domain = $subdomain ? $subdomain.'.'.$domain : $domain; $url = 'http://'.str_replace($domain.'.'.$tld, $language.'.'.$domain.'.'.$tld, $_SERVER['HTTP_HOST']).$_SERVER['REQUEST_URI']; $context-&gt;getController()-&gt;redirect($url); break; } } } $context-&gt;dispatch(); </code></pre> <p><strong>Update</strong> Solution: Custom filter</p> <pre><code>&lt;?php class subdomainFilter extends sfFilter { public function execute($filterChain) { $context = $this-&gt;getContext(); $user = $this-&gt;getContext()-&gt;getUser(); $request = $this-&gt;getContext()-&gt;getRequest(); // get the domain parts as an array $host = array_reverse(explode('.', $request-&gt;getHost())); list($tld, $domain) = $host; $subdomain2 = $host[3]; $subdomain = $host[2]; // determine which subdomain we're looking at $app = ($host[2] == 'staging') ? $subdomain2 : $subdomain; if(empty($app) || $app == 'www') { // if first time if ($this-&gt;isFirstCall()) { $browser_languages = $request-&gt;getLanguages(); // set default lang, for API as CURL doesn't set the language $lang = sfConfig::get('app_default_culture'); foreach($browser_languages as $language) { $allowed_culture = in_array($language, sfConfig::get('app_languagesAvailable')); if($allowed_culture) { $lang = $language; break; } } }else { // Get user culture $lang = $user-&gt;getCulture(); } $domain = $subdomain ? $subdomain.'.'.$domain : $domain; $url = str_replace($domain.'.'.$tld, $lang.'.'.$domain.'.'.$tld, $request-&gt;getURI()); $context-&gt;getController()-&gt;redirect($url); } $filterChain-&gt;execute(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload