/** * Redirect to the route stored in $this->redirect */ public function redirect() { if (!$this->redirect) { return; } $base = $this->admin->base; $this->redirect = '/' . ltrim($this->redirect, '/'); $multilang = $this->isMultilang(); $redirect = ''; if ($multilang) { // if base path does not already contain the lang code, add it $langPrefix = '/' . $this->grav['session']->admin_lang; if (!Utils::startsWith($base, $langPrefix . '/')) { $base = $langPrefix . $base; } // now the first 4 chars of base contain the lang code. // if redirect path already contains the lang code, and is != than the base lang code, then use redirect path as-is if (Utils::pathPrefixedByLangCode($base) && Utils::pathPrefixedByLangCode($this->redirect) && substr($base, 0, 4) != substr($this->redirect, 0, 4)) { $redirect = $this->redirect; } else { if (!Utils::startsWith($this->redirect, $base)) { $this->redirect = $base . $this->redirect; } } } else { if (!Utils::startsWith($this->redirect, $base)) { $this->redirect = $base . $this->redirect; } } if (!$redirect) { $redirect = $this->redirect; } $this->grav->redirect($redirect, $this->redirectCode); }
public function testPathPrefixedByLangCode() { $languagesEnabled = $this->grav['config']->get('system.languages.supported', []); $arrayOfLanguages = ['en', 'de', 'it', 'es', 'dk', 'el']; $languagesNotEnabled = array_diff($arrayOfLanguages, $languagesEnabled); $oneLanguageNotEnabled = reset($languagesNotEnabled); if (count($languagesEnabled)) { $this->assertTrue(Utils::pathPrefixedByLangCode('/' . $languagesEnabled[0] . '/test')); } $this->assertFalse(Utils::pathPrefixedByLangCode('/' . $oneLanguageNotEnabled . '/test')); $this->assertFalse(Utils::pathPrefixedByLangCode('/test')); $this->assertFalse(Utils::pathPrefixedByLangCode('/xx')); $this->assertFalse(Utils::pathPrefixedByLangCode('/xx/')); $this->assertFalse(Utils::pathPrefixedByLangCode('/')); }