P
P
Prosspa2020-05-20 10:00:19
Yii
Prosspa, 2020-05-20 10:00:19

How to make a redirect to the current page with new attributes?

It is necessary to make it so that when the user enters the site, he is immediately set to the version of the language that is preferred in the browser.
I can't figure out how to do a redirect.
For example, there is a test.ru domain , after the user has landed on the page, send it or put a link like test.ru/ru (immediately after loading).
But I still don’t understand how to properly set up a redirect of this kind, I have either TO_MENY_REDIRECTS or go to the browser in a cyclic reload ...

Who faced such a task? Can you please tell me how to solve this problem?
For I don’t come across this often, and Google didn’t give anything intelligible about this. Issued only switching through the switch. But I already have a switch on my site, all I need is what I described above.
I will attach the class method code:

public function redirectUrl()
    {
        $lang = $this->getBestMatch();
        $uri = $_SERVER['REQUEST_URI'];
        if( Yii::$app->request->cookies->has('current_language_id')) {
            $lang_id = Yii::$app->request->cookies->get('current_language_id')->value;
            $currentLang = Lang::findOne($lang_id);
            $newLang = Lang::findOne(['url' => $lang]);
            Yii::$app->response->cookies->add(new \yii\web\Cookie([
                'name' => 'current_language_id',
                'value' => $newLang->id
            ]));
            $parseUri = $lang.$uri;
            if(strpos($uri, $lang)) {
                $parseUri = str_replace($currentLang->url,$newLang->url,$uri);
            }
            $url = Yii::$app->urlManager->createAbsoluteUrl('/');
//            Yii::$app->response->redirect($url);
            return 1;
        } else {
            $lang_id = Yii::$app->request->cookies->get('current_language_id')->value;
            $currentLang = Lang::findOne($lang_id);
            $newLang = Lang::findOne(['url' => $lang]);
            Yii::$app->response->cookies->add(new \yii\web\Cookie([
                'name' => 'current_language_id',
                'value' => $newLang->id
            ]));
            $parseUri = $lang.$uri;
            if(strpos($uri, $lang)) {
                $parseUri = str_replace($currentLang->url,$newLang->url,$uri);
            }
            $url = Yii::$app->urlManager->createAbsoluteUrl($parseUri);
//            Yii::$app->response->redirect($url);
        }
        return false;
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2020-05-20
@Prosspa

If you do not want to use ready-made extensions, there is an article that tells you step by step how to organize multilingualism on the site.

T
ThunderCat, 2020-05-20
@ThunderCat

TL;DR
there you have porridge, because of this and garbage. Check separately - if the url already has a language , then you only need to update the cookies and what else of the variables is there, in other cases already determine the desired language and redirect.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question