<?phpnamespace MultilingualBundle\Controller;use Pimcore\Controller\FrontendController;use Pimcore\Model\Document;use Pimcore\Tool;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\HttpKernel\Event\FilterControllerEvent;use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;class DefaultController extends FrontendController{ /** * @throws NotFoundHttpException */ public function goToFirstChildAction() { if ($this->document->hasChildren()) { $children = $this->document->getChildren(); $firstChild = reset($children); return $this->redirect($firstChild->getFullPath()); } else { throw new NotFoundHttpException('No children found. Could not redirect to first child.'); } } /** * @throws NotFoundHttpException */ public function languageDetectionAction(Request $request) { // $locale = $request->getLocale(); $languages = Tool::getValidLanguages(); $locale = reset($languages); if(isset($_SERVER['HTTP_USER_AGENT'])){ if(!preg_match('/bot|crawl|slurp|spider|Guzzle|mediapartners/i', $_SERVER['HTTP_USER_AGENT'])){ if($_SERVER['HTTP_ACCEPT_LANGUAGE']){ $locale = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2); } } } $browserLanguage = $locale; // $languageArr = explode('_', locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE'])); // $browserLanguage = reset($languageArr); // return new Response($locale.' '.$browserLanguage); // Check if the browser language is a valid frontend language if (in_array($browserLanguage, $languages)) { $language = $browserLanguage; } else { // If it is not, take the first frontend language as default $language = reset($languages); } // Get the folder of the current language (in the current site) $currentSitePath = $this->document->getRealFullPath(); // check language $publishedLanguages = []; foreach ($languages as $foundLanguage){ $languageDoc = Document\Page::getByPath($currentSitePath . '/' . $foundLanguage); if(!$this->editmode){ if($languageDoc->isPublished()){ // check if document is published $publishedLanguages[] = $foundLanguage; } } } if(count($publishedLanguages) === 1){ $reDirectCorrect = 301; }else{ $reDirectCorrect = 302; // when multiple language are found } $folder = Document\Page::getByPath($currentSitePath . '/' . $language); if ($folder) { $document = $this->findFirstDocumentByParentId($folder->getId()); if ($document) { return $this->redirect($document->getPath() . $document->getKey(), $reDirectCorrect); } else { throw new NotFoundHttpException('No document found in your browser language'); } } else { throw new NotFoundHttpException('No language folder found that matches your browser language'); } } /** * Find the first active document for a given folder * * @param $folder_id * @return mixed */ protected function findFirstDocumentByParentId($folder_id) { $list = new Document\Listing(); $list->setCondition("parentId = ?", (int)$folder_id); $list->setOrderKey("index"); $list->setOrder("asc"); $list->setLimit(1); $childrenList = $list->load(); return reset($childrenList); }}