src/AppBundle/Controller/LocalizationController.php line 22

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use AppBundle\Form\LocaleType;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  6. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. class LocalizationController extends Controller
  10. {
  11.     /**
  12.      * @Route("/locale", name="app_locale")
  13.      * @Template("_locale.html.twig")
  14.      *
  15.      * @param Request $request
  16.      *
  17.      * @return array|RedirectResponse
  18.      */
  19.     public function locale(Request $request)
  20.     {
  21.         $form $this->createForm(LocaleType::class, null, [
  22.             'action' => $this->generateUrl('app_locale'),
  23.             'method' => 'POST',
  24.         ]);
  25.         $form->get('locale')->setData($request->getLocale());
  26.         $form->handleRequest($request);
  27.         if ($form->isSubmitted() && $form->isValid()) {
  28.             $data $form->getData();
  29.             if ($user $this->getUser()) {
  30.                 $user->setLocale($data['locale']);
  31.                 $entityManager $this->getDoctrine()->getManager();
  32.                 $entityManager->persist($user);
  33.                 $entityManager->flush();
  34.             }
  35.             return $this->redirectToRoute('app_home_page', [
  36.                 '_locale' => $data['locale'],
  37.             ]);
  38.         }
  39.         // dd($form);
  40.         return [
  41.             'form' => $form->createView(),
  42.         ];
  43.     }
  44. }