Answer the question
In order to leave comments, you need to log in
How to add multilingual support to Yii2 - authClient returnUrl?
Good day!
There is a project in which multilingualism is implemented, according to this article
Those . links look like https://site.io/ru/profile | https://site.io/en/profile | etc.
There is a Yii2 extension - authClient .
In the settings for the extension, there is a returnUrl parameter that stores the link
to which the user is redirected after successful authorization/registration.
But here's the problem, when I redirect to https://site.io/auth?authclient=social_network_name
,
I get an error:
Exception – yii\authclient\InvalidResponseException
Request failed with code: 401,
message: {"error":"invalid_grant","error_description":"Code is invalid or expired."}
public function actions() {
return [
'auth' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'onAuthSuccess'],
],
...
];
}
public function onAuthSuccess($client) {
$exploded_url = explode(Yii::$app->request->hostInfo . '/', $client->getReturnUrl());
$current_lang = Lang::getCurrent();
$new_return_url = Yii::$app->request->hostInfo . '/' . $current_lang->url . '/' . $exploded_url[1];
$client->setReturnUrl($new_return_url);
if ( (new AuthHandler($client))->handle() ) {
$this->redirect('profile');
}
}
Answer the question
In order to leave comments, you need to log in
I "translated" all urls with this extension: https://github.com/codemix/yii2-localeurls
Any request goes through urlManager which is overridden in this extension. The language for the user is automatically determined and redirected to this language version. You can also add to "ignore" those url patterns that do not need to be processed and redirected. For example, /api etc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question