]> Raphaƫl G. Git Repositories - blogbundle/blob - Controller/DefaultController.php
Fix description
[blogbundle] / Controller / DefaultController.php
1 <?php
2
3 namespace Rapsys\BlogBundle\Controller;
4
5 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
7 class DefaultController extends Controller {
8 /**
9 * Redirect on first supported language version
10 */
11 public function rootAction() {
12 //Set default locale
13 $locale = 'en';
14
15 //Supported application languages
16 $supportedLanguage = explode('|', $this->getParameter('blog.locales'));
17
18 //Language list
19 if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
20 //Init array
21 $httpAcceptLanguage = [];
22
23 //Extract languages
24 foreach(explode(',',str_replace('-','_',$_SERVER['HTTP_ACCEPT_LANGUAGE'])) as $candidate) {
25 //Extract candidate and optional weight
26 @list($candidate, $weight) = explode(';', $candidate);
27 if (!empty($candidate)) {
28 $httpAcceptLanguage[!empty($weight)?$weight:1][] = $candidate;
29 }
30 }
31
32 //Find first match
33 if (!empty($httpAcceptLanguage)) {
34 foreach($httpAcceptLanguage as $weight => $candidates) {
35 if (($candidate = array_intersect($candidates, $supportedLanguage)) && ($candidate = reset($candidate))) {
36 $locale = $candidate;
37 break;
38 }
39 }
40 }
41 }
42
43 //Redirect to localised homepage
44 return $this->redirectToRoute('homepage', array('_locale' => $locale), 302);
45 }
46 }