I set the cookie so

public function actionCountry(){ $name=Yii::$app->request->get("name","Russia"); $cookies = Yii::$app->response->cookies; // добавление новой куки в HTTP-ответ $cookies->add(new \yii\web\Cookie([ 'name' => 'country', 'value' => $name, ])); // $session=\Yii::$app->session; //$session->set('country',$name); //$session->remove('name'); return $this->render('country',[ 'name'=>$name, 'cookies'=>$cookies->getValue('country') // 'session'=>\Yii::$app->session->get('country') ]); } } 

I treat it like this

 public function actionIndex() { $cookies= \Yii::$app->request->cookies; return $this->render('index',[ 'cookies'=>$cookies->getValue('country') //'session'=>\Yii::$app->session->get('country') ]); } 

I deduce so

 <p> Страна <a href="<?= Yii::$app->urlManager->CreateUrl(['country/country','name'=>$cookies]) ?>"> <?=$cookies?></a></p> 

The cookie is not created well, and therefore is not displayed. From where I know that the web developer is not being created, it does not display what the problem is. I have already reached the point that I have already simply created a country cookie through the developer and I am trying to withdraw it through

 var_dump(Yii::$app->request->cookies['country']); 

returns null

shorter than the standard cookie, it outputs via request but does not generate a response

  • By default, in Yii2 $ path = '/'; Is this a problem? - Invision
  • @Invision so I don’t touch the path like it - Sergalas
  • one
    so I do not see the full picture of what is happening) that what you are demonstrating is the same as the documentation. It is necessary to dig towards a server configuration. Does $ _COOKIE raw work? Try methods in the framework itself, find where it falls off. - Invision
  • The @Invision trick is that he creates a cookie there with some kind of confusing parameter. And she finds it. but create your own no. In addition, sessions are not being created yet - Sergalas

1 answer 1

Your code is correct, the problem is definitely in the settings or environment.

  • Check out Yii2 . Use the standard response from yii2 . If you use your own, replace it with standard time and check whether it will work with the standard one, without any special settings.

  • Check php . If you are not creating a session, session.use_cookies is probably 0 .

  • Check the веб-сервер . Execute a simple php-file only with the installation of cookies and check that it is successfully created.

Set a cookie and see the result in the browser console:

 <?php setcookie('foo','bar',time()+60*60*24*365, '/'); ?>