Why doesn't cookies (Yii2)?

There domain1.com and domain2.com
with domain1.com it sends a request to Ajax domain2.com on the page /ajax/cookie
$cookies = Yii::$app->request->cookies;
 if (!$cookies->has($is cookiename)) {
 Yii::$app->response->cookies->add(new \yii\web\Cookie([
 'name' => $is cookiename,
 'value' => 'test',
 'expire' => time () + 3600 * 24 * 365
]));
 } else {
 $value = Yii::$app->response->cookies->get($is cookiename);
 }


cookies are not installed , you can do to install? if so, what's wrong?
March 12th 20 at 08:07
1 answer
March 12th 20 at 08:09
Solution
Good afternoon.
Slightly corrected Your code.
$cookies = Yii::$app->request->cookies;
if (!$cookies->has($is cookiename)) {
 Yii::$app->response->cookies->add(new \yii\web\Cookie([
 'name' => $is cookiename,
 'value' => 'test',
 'expire' => time () + 3600 * 24 * 365
]));
} else {
 $value = $cookies->get($is cookiename);
}

It sets the cookies
Yii::$app->response->cookies
and it reads
Yii::$app->request->cookies
The difference in the "response" and "request"
doesn't work( every time a new one is installed., that is, does not find a previously created, the domain value may still need to specify? - erwin commented on March 12th 20 at 08:12
although it is also set domain1 doesn't work either - erwin commented on March 12th 20 at 08:15
@erwin, as You wrote in a comment @webinar, check the variable $is cookiename.
When You are trying to set cookies?
Show more code. - Harvey commented on March 12th 20 at 08:18
@Harvey, $is cookiename = md5('model_Widget_' . $this->id); - erwin commented on March 12th 20 at 08:21
@erwin, well, what next? where the rest of the code?
Where I declare a variable and where check cookies? - Harvey commented on March 12th 20 at 08:24
@erwin, check now $this->id that it get here? If every time a new value, it will create a new cookies. - Harvey commented on March 12th 20 at 08:27
in the model Widget
public function getCookieName()
{
 return md5('model_Widget_' . $this->id); 
}

public function getCookie()
{
 $is cookiename = $this->is cookiename;
 $value = null;

 $cookies = Yii::$app->request->cookies;
 if (!$cookies->has($is cookiename)) {
 $value = 'test';
 Yii::$app->response->cookies->add(new \yii\web\Cookie([
 'name' => $is cookiename,
 'value' => $value,
 'expire' => time() + 3600 * 24 * 365 * 10
]));
 } else {
 $value = $cookies->get($is cookiename);
}

 return $value;
 }


in the controller there is a sample $model = Widget::findOne($id)
next is selecting $model->cookie should return the previously set cookie, that is, $this->id = $id and all the time the same - erwin commented on March 12th 20 at 08:30
@erwin, try to check to make sure
return md5('model_Widget_' . 123);
After that will re-set cookies?
If not, then this is the problem. - Harvey commented on March 12th 20 at 08:33
@Harvey, Yes, still a new set, I think there is still the fact that I was Ajax trying to send request from first domain to the second, and expect to ustanovleny cookies if re-treatment can in this case? - erwin commented on March 12th 20 at 08:36
@erwin, verify that ajax works. - Harvey commented on March 12th 20 at 08:39
@Harvey, I checked really domain2.there are also sets new all the time, weird in General - erwin commented on March 12th 20 at 08:42
although, in the domain works when you call Ajax from another domain not working - erwin commented on March 12th 20 at 08:45
or will have to work with cookies in js? - erwin commented on March 12th 20 at 08:48
@erwin, if you work with cookies in js, then he is yii2 will not handle cookies. I don't remember exactly, but this is due to a security constraint.
You didn't specify in the question, why using ajax are You doing this?
Most likely You have the wrong implementation of Your ideas. - Harvey commented on March 12th 20 at 08:51
@Harvey, look I need to display on domain1 widget , the widget is in domain2. there are settings in some cases deduce, therefore, appeal to domain2 for a particular url and displays a widget, it's only for ajax, as all sorts of services that give the js code for installation on site

I hope could explain what I want

p.s can no longer comment, writes limit( - erwin commented on March 12th 20 at 08:54
@erwin, in any case, the code works, You its a little tweaked. The problem You have in the other.
To do this, create a new question where you describe and what You have difficulties.
This question is close, mark solution. - Harvey commented on March 12th 20 at 08:57

Find more questions by tags HTTP CookiesYii