Skip to content

Commit

Permalink
keep get params while redirecting close #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Naumov committed May 27, 2016
1 parent 7c4d4f2 commit ecc6e0a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage
tests/runtime
coverage.xml
.vagrant
.idea/
28 changes: 13 additions & 15 deletions src/LanguageEvents/GettingLanguageByUrl.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace DevGroup\Multilingual\LanguageEvents;

use DevGroup\Multilingual\models\Language;
use yii\helpers\ArrayHelper;

class GettingLanguageByUrl implements GettingLanguage, AfterGettingLanguage
{

public static function gettingLanguage(languageEvent $event)
{
if ($event->currentLanguageId === false) {
Expand Down Expand Up @@ -36,7 +37,6 @@ public static function gettingLanguage(languageEvent $event)

public static function afterGettingLanguage(languageEvent $event)
{

$languageMatched = $event->languages[$event->multilingual->language_id];
if ($event->needRedirect === true && $languageMatched->folder) {
if ($languageMatched->folder === $event->request->pathInfo) {
Expand All @@ -46,10 +46,11 @@ public static function afterGettingLanguage(languageEvent $event)
// no matched language and not in excluded routes - should redirect to user's regional domain with 302
\Yii::$app->urlManager->forceHostInUrl = true;
$event->redirectUrl = \Yii::$app->urlManager->createUrl(
[
$event->request->pathInfo,
'language_id' => $event->multilingual->language_id
]
ArrayHelper::merge(
[$event->request->pathInfo],
\Yii::$app->request->get(),
['language_id' => $event->multilingual->language_id]
)
);
\Yii::$app->urlManager->forceHostInUrl = false;
$event->redirectCode = 302;
Expand All @@ -60,16 +61,13 @@ public static function afterGettingLanguage(languageEvent $event)
// no matched language and not in excluded routes - should redirect to user's regional domain with 302
\Yii::$app->urlManager->forceHostInUrl = true;
$event->redirectUrl = $event->sender->createUrl(
[
$event->request->pathInfo,
'language_id' => $event->multilingual->language_id
]
ArrayHelper::merge(
[$event->request->pathInfo],
\Yii::$app->request->get(),
['language_id' => $event->multilingual->language_id]
)
);
$event->redirectCode = 302;
}


}


}
}
20 changes: 19 additions & 1 deletion tests/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected function setUp()
'class' => Connection::className(),
'dsn' => 'mysql:host=localhost;dbname=multilingual.dev',
'username' => 'root',
'password' => '7896321',
'password' => '',
]);

Yii::$app->getDb()->open();
Expand Down Expand Up @@ -518,6 +518,24 @@ public function testPreferredCountryByGeo()
$this->assertEquals(null, $multilingual->getPreferredCity());
}

public function testGetData()
{
/** @var \DevGroup\Multilingual\Multilingual $multilingual */
Yii::$app->trigger(Application::EVENT_BEFORE_REQUEST);
$_SERVER['SERVER_NAME'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/site/about?post=1';
$_GET = ['post' => 1];
try {
$this->resolve();
} catch (ExitException $e) {
$this->assertArraySubset(
['location' => ['http://example.ru/site/about?post=1']],
Yii::$app->response->getHeaders()->toArray()
);
$this->assertEquals(302, Yii::$app->response->statusCode);
}

}

/**
* Resets Yii2 Request component so it can handle another fake request and resolves it
Expand Down

0 comments on commit ecc6e0a

Please sign in to comment.