/** * @param LocaleEvent $event * @return ResponseInterface|void */ public function found(LocaleEvent $event) { $locale = $event->getLocale(); $request = $event->getRequest(); if (!$this->isHttpRequest($request)) { return; } /** @var HttpRequest $request */ $cookie = $request->getCookie(); $cookieName = $this->getCookieName(); // Omit Set-Cookie header when cookie is present // and Expires does not need renewing... if ($this->getCookieExpiration() === null && $cookie instanceof CookieHeader && $cookie->offsetExists($cookieName) && $locale === $cookie->offsetGet($cookieName)) { return; } $cookiePath = '/'; if (method_exists($request, 'getBasePath')) { $cookiePath = rtrim($request->getBasePath(), '/') . '/'; } $setCookie = new SetCookieHeader($cookieName, $locale, $this->getCookieExpiration(), $cookiePath); /** @var HttpResponse $response */ $response = $event->getResponse(); $response->getHeaders()->addHeader($setCookie); }
public function found(LocaleEvent $event) { $locale = $event->getLocale(); $request = $event->getRequest(); $cookieName = $this->getCookieName(); if (!$this->isHttpRequest($request)) { return; } $cookie = $request->getCookie(); // Omit Set-Cookie header when cookie is present if ($cookie instanceof Cookie && $cookie->offsetExists($cookieName) && $locale === $cookie->offsetGet($cookieName)) { return; } $path = '/'; if (method_exists($request, 'getBasePath')) { $path = rtrim($request->getBasePath(), '/') . '/'; } $response = $event->getResponse(); $setCookie = new SetCookie($cookieName, $locale, null, $path); $response->getHeaders()->addHeader($setCookie); }
public function found(LocaleEvent $event) { $request = $event->getRequest(); $assetManager = $this->getServiceLocator()->getServiceLocator()->get('AssetManager\\Service\\AssetManager'); $request = $event->getRequest(); if ($assetManager->resolvesToAsset($request)) { return; } if (!$this->isHttpRequest($request)) { return; } $locale = $event->getLocale(); if (null === $locale) { return; } if (!$this->redirectToCanonical() && null !== $this->getAliases()) { $alias = $this->getAliasForLocale($locale); if (null !== $alias) { $locale = $alias; } } $base = $this->getBasePath(); $found = $this->getFirstSegmentInPath($request->getUri(), $base); $this->getRouter()->setBaseUrl($base . '/' . $locale); if ($locale === $found) { return; } if (!$this->redirectWhenFound()) { return; } $uri = $request->getUri(); $path = $uri->getPath(); if (!$found || $event->hasSupported() && !in_array($found, $event->getSupported())) { $path = $base . '/' . $locale . substr($path, strlen($base)); } else { $path = str_replace($found, $locale, $path); } $uri->setPath($path); $response = $event->getResponse(); $response->setStatusCode(self::REDIRECT_STATUS_CODE); $response->getHeaders()->addHeaderLine('Location', $uri->toString()); return $response; }
public function found(LocaleEvent $event) { $request = $event->getRequest(); if (!$this->isHttpRequest($request)) { return; } if (!$event->hasSupported()) { return; } $locale = $event->getLocale(); if (null === $locale) { return; } // By default, use the alias to redirect to if (!$this->redirectToCanonical()) { $locale = $this->getAliasForLocale($locale); } $host = str_replace(self::LOCALE_KEY, $locale, $this->getDomain()); $uri = $request->getUri(); if ($host === $uri->getHost()) { return; } $uri->setHost($host); $response = $event->getResponse(); $response->setStatusCode(self::REDIRECT_STATUS_CODE); $response->getHeaders()->addHeaderLine('Location', $uri->toString()); return $response; }
public function found(LocaleEvent $event) { $request = $event->getRequest(); if (!$this->isHttpRequest($request)) { return; } $locale = $event->getLocale(); if (null === $locale) { return; } if (!$this->redirectToCanonical() && null !== $this->getAliases()) { $alias = $this->getAliasForLocale($locale); if (null !== $alias) { $locale = $alias; } } $base = $this->getBasePath(); $found = $this->getFirstSegmentInPath($request->getUri(), $base); $this->getRouter()->setBaseUrl($base . '/' . $locale); if ($locale === $found) { return; } if (!$this->redirectWhenFound()) { return; } $uri = $request->getUri(); $path = $uri->getPath(); if (!$found || $event->hasSupported() && !in_array($found, $event->getSupported())) { $path = '/' . $locale . $path; } else { $path = preg_filter('/^\\/(.+?)(\\/.+)/', '/' . $locale . '$2', $path); } $uri->setPath($path); $response = $event->getResponse(); $response->setStatusCode(self::REDIRECT_STATUS_CODE); $response->getHeaders()->addHeaderLine('Location', $uri->toString()); return $response; }