Answer the question
In order to leave comments, you need to log in
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
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);
}
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 questionAsk a Question
731 491 924 answers to any question