D
D
dev1232020-03-10 16:01:22
Yii
dev123, 2020-03-10 16:01:22

How to send a specific cookie in every response from the server?

New versions of Google Chrome have now begun to actively require the SameSite cookie and if it is not present, then cookies are not accepted from the site.
There is a site on Yii 2.0.13. How to make sure that SameSite cookie is added to every response from the server? It needs to be done centrally in one place. In each action, adding this cookie manually is completely stupid.
I know that in the latest versions of Yii2 this feature has already been implemented. But I don't want to update the site to the latest version. it just simply can stop working normally (already tried, errors rained down).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2020-03-13
@kimono

You need to listen for an event after the request is processed and add a cookie to the response in the application configuration:

$config = [
    'components' => [],
    'modules' => [],
    'on afterRequest' => function (Event $event) {
        Yii::$app->response->cookie->add(new Cookie([
            'name' => 'cookie_name',
            'value' => 'cookie_value'
        ]));
    }
];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question