/** * Forcing the request locale if the user entity uses a custom locale that is set on the entity * * @param InteractiveLoginEvent $event */ public function onLogin(InteractiveLoginEvent $event) { $user = $event->getAuthenticationToken()->getUser(); // Skipping login that is not coming from the backend User entity if (false == $user instanceof User) { return; } if ($locale = $user->getLocale()) { $event->getRequest()->setLocale($locale); } // Redirection on the first application to which the User has access $appRepo = $this->doctrine->getRepository('UnifikSystemBundle:App'); $apps = $appRepo->findAllHasAccess($this->securityContext, $user->getId()); if ($apps && !in_array($apps[0]->getId(), array(AppRepository::FRONTEND_APP_ID, AppRepository::BACKEND_APP_ID))) { $event->getRequest()->request->set('_target_path', $this->router->generate('unifik_system_backend_switch_managed_app', array('appSlug' => $apps[0]->getSlug()), UrlGeneratorInterface::ABSOLUTE_URL)); } }
/** * Setting lang seo alternatives * * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { $langs = ['en', 'ru']; $request = $event->getRequest(); $route = $request->attributes->get('_route'); if ($route) { $routeParams = $request->attributes->get('_route_params'); $attributes = $request->query->all(); if (!is_null($routeParams)) { $attributes = array_merge($attributes, $routeParams); } foreach ($langs as $lang) { $attributes['_locale'] = $lang; $this->seoPage->addLangAlternate($this->router->generate($route, $attributes, true), $lang); } } }
public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); $route = $request->get('_route'); $locale = $request->get('_locale'); $route_params = $request->attributes->get('_route_params'); //menu locale switcher used? if ($request->query->has('force-locale-switch')) { $request->cookies->set('hl', $locale); $url = $this->router->generate($route, ['_locale' => $locale] + $route_params); $event->setResponse(new RedirectResponse($url)); return; } //first request the cookie is empty. Gets auto set by I18nRouter if ($request->cookies->get('hl')) { return; } $preferredLocale = $request->getPreferredLanguage($this->availableLocals); if ($preferredLocale && $request->attributes->get('_locale') != $preferredLocale) { $url = $this->router->generate($route, ['_locale' => $preferredLocale] + $route_params); $event->setResponse(new RedirectResponse($url)); } }
/** * Fallback * * Fallback to the parent if it exists or to the current Section in the Core * * @param $element * @param $locale * * @return mixed */ protected function fallBack($element, $locale) { if ($parent = $element->getParent()) { $this->setElement($parent); $data = $this->generate(); $url = $data[$locale->getCode()]['url']; } elseif (false == $element instanceof Section) { $this->setElement($this->core->getSection()); $data = $this->generate(); $url = $data[$locale->getCode()]['url']; } else { // Fallback to the homepage try { $url = $this->router->generate('section_id_1', array('_locale' => $locale->getCode())); } catch (\Exception $e) { $url = ''; } } return $url; }