]> Raphaƫl G. Git Repositories - blogbundle/blob - Controller/DefaultController.php
Add favicon
[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 //Redirect on first supported language version
9 public function rootAction() {
10 //Set default locale
11 $locale = 'en';
12
13 //Supported application languages
14 //XXX: Array for route validation is not supported by default (rapsys patch)
15 $supportedLanguage = $this->getParameter('blog.locales');
16
17 //Language list
18 if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
19 //Init array
20 $httpAcceptLanguage = [];
21
22 //Extract languages
23 foreach(explode(',',str_replace('-','_',$_SERVER['HTTP_ACCEPT_LANGUAGE'])) as $candidate) {
24 //Extract candidate and optional weight
25 @list($candidate, $weight) = explode(';', $candidate);
26 if (!empty($candidate)) {
27 $httpAcceptLanguage[!empty($weight)?$weight:1][] = $candidate;
28 }
29 }
30
31 //Find first match
32 if (!empty($httpAcceptLanguage)) {
33 foreach($httpAcceptLanguage as $weight => $candidates) {
34 if (($candidate = array_intersect($candidates, $supportedLanguage)) && ($candidate = reset($candidate))) {
35 $locale = $candidate;
36 break;
37 }
38 }
39 }
40 }
41
42 //Redirect to localised homepage
43 return $this->redirectToRoute('homepage', array('_locale' => $locale), 302);
44 }
45 }