src/Controller/MainController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  7. use App\Service\LocaleSubscriber;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. /**
  11. * @Route("/{_locale?fr}",locale="fr", requirements={
  12. * "_locale": "en|fr",
  13. * "_format": "html|xml",
  14. * })
  15. */
  16. class MainController extends AbstractController
  17. {
  18. /**
  19. * @Route("/", name="home")
  20. */
  21. public function home()
  22. {
  23. return $this->render('main/index.html.twig', [
  24. 'controller_name' => 'MainController',
  25. ]);
  26. }
  27. /**
  28. * @Route("/main", name="main")
  29. */
  30. public function index()
  31. {
  32. return $this->render('main/index.html.twig', [
  33. 'controller_name' => 'MainController',
  34. ]);
  35. }
  36. /**
  37. * @Route("/ch/{local}", name="ch")
  38. */
  39. public function chan(LocaleSubscriber $locale,$local, Request $request)
  40. {
  41. // $locale->change($local,$request);
  42. $referer = $request->headers->get('referer');
  43. return new RedirectResponse($referer);
  44. }
  45. /* public function onKernelRequest(RequestEvent $event)
  46. {
  47. $request = $event->getRequest();
  48. // some logic to determine the $locale
  49. $request->setLocale($locales);
  50. }*/
  51. /**
  52. * @Route("/change_locale/{local}",name="change_locale")
  53. */
  54. public function changeLocale(SessionInterface $session,Request $request,String $local)
  55. {
  56. // $locale = $session->get('_locale');
  57. // $locale = $session->set('_locale',$local);
  58. $locale = $request->getLocale();
  59. $locale = $request->setLocale($local);
  60. //$referer = $request->headers->get('referer');
  61. //return new RedirectResponse("$referer");
  62. $route=$request->attributes->get('_route');
  63. return $this->redirectToRoute($route,[
  64. '_locale'=>$local,
  65. 'local'=>$local,
  66. ]);
  67. }
  68. }