E
E
EvgMul2019-01-27 15:59:36
Yii
EvgMul, 2019-01-27 15:59:36

How to configure Yii2 so that username is always passed in http request headers?

Hello. Actually the whole question is described in the title. Can you please tell me if there is a setting in yii where the username of the logged in user will always be transmitted in the headers? Well, or if the user is not authorized, then for example, guest?
Thanks in advance to all who respond.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2019-01-28
@EvgMul

Do beforeAction in the config or in the general controller and cram any headers

public function beforeAction($action)
{
    $headers = Yii::$app->response->headers;
    $user_id = Yii::$app->user->isGuest ? 'guest' : Yii::$app->user->id;
    $headers->set('user_id', $user_id);
    return parent::beforeAction($action);
}

NB: Perhaps you need not beforeAction, but afterAction. But there will be the same slippers, only after.

D
Dmitry Bay, 2019-01-27
@kawabanga

What is a header? If you mean the Title section, then open the layouts/main.php template
and in the title section add <?= !Yii::$app->user->isGuest? Yii::$app->user->username : 'Guest,' ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question