I am using lajax/yii2-translate-manager and lajax/yii2-language-picker After server upgrade to PHP version 8.1 switching to another language throws an error
An Error occurred while handling another error:
ValueError: setcookie(): "path" option cannot contain ",", ";", " ", "\t", "\r", "\n",
"\013", or "\014" in /app/vendor/yiisoft/yii2/web/Response.php:404
Stack trace:
#0 /app/vendor/yiisoft/yii2/web/Response.php(404): setcookie()
#1 /app/vendor/yiisoft/yii2/web/Response.php(381): yii\web\Response->sendCookies()
#2 /app/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#3 /app/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yii\web\Response->send()
#4 /app/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler-
>renderException()
#5 [internal function]: yii\base\ErrorHandler->handleException()
#6 {main}
Previous exception:
ValueError: setcookie(): "path" option cannot contain ",", ";", " ", "\t", "\r", "\n",
"\013", or "\014" in /app/vendor/yiisoft/yii2/web/Response.php:404
Stack trace:
#0 /app/vendor/yiisoft/yii2/web/Response.php(404): setcookie()
#1 /app/vendor/yiisoft/yii2/web/Response.php(381): yii\web\Response->sendCookies()
#2 /app/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#3 /app/vendor/yiisoft/yii2/base/Application.php(392): yii\web\Response->send()
#4 /app/frontend/web/index.php(17): yii\base\Application->run()
#5 {main}
I see that there is some cookie issue but dont know what causes this and how to fix this. Maby someone have atleast idea what to look for?
I found solution - there is new approach how to set language cookie in Yii2 newest version. In frontend(or backend - where you are using Yii2 lajax/yii2-translate-manager) -> components -> LanguageSelector.php need to rewrite code
class LanguageSelector implements BootstrapInterface
{
public $supportedLanguages = [];
/**
* @param Application $app
*/
public function bootstrap($app)
{
// ignore lang parameter if language picker has been selected
if (!empty($app->request->get('language-picker-language'))) {
$languageCookie = new Cookie([
'name' => 'language',
'value' => $app->request->get('language-picker-language'),
'expire' => strtotime('+30 days'),
'path' => '/',
'secure' => true,
'sameSite' => 'None', //Select: Lax, None or Strict
]);
$app->response->cookies->add($languageCookie);
return;
}
// get language from request
$preferredLanguage = $app->request->get('lang');
if (strlen($preferredLanguage) === 2) {
$preferredLanguage .= '-' . strtoupper($preferredLanguage);
}
if (!empty($preferredLanguage)) {
$languageCookie = new Cookie([
'name' => 'language',
'value' => $preferredLanguage,
'expire' => strtotime('+30 days'),
'path' => '/',
'secure' => true,
'sameSite' => 'None', ////Select: Lax, None or Strict
]);
$app->response->cookies->add($languageCookie);
}
// get language from cookie
if (empty($preferredLanguage)) {
$preferredLanguage = isset($app->request->cookies['language']) ? $app->request->cookies['language']->value : null;
}
// get language from browser
if (empty($preferredLanguage)) {
$preferredLanguage = $app->request->getPreferredLanguage(array_keys(Language::getLanguageNames(true)));
}
$app->language = $preferredLanguage;
}
}