简体   繁体   English

无法在 CakePHP 4 中使用 setUser 在 Auth 中设置用户

[英]Not able to set user in Auth using setUser in CakePHP 4

In CakePHP 4.0在 CakePHP 4.0 中

When I'm trying to do $this->Auth->setUser($user) , I'm getting this error:当我尝试执行$this->Auth->setUser($user) ,出现此错误:

\\cakephp\\cakephp\\src\\Http\\Session.php - Argument 1 passed to Cake\\Http\\Session::_overwrite() must be of the type array protected function _overwrite(array &$old, array $new): void \\cakephp\\cakephp\\src\\Http\\Session.php - 传递给 Cake\\Http\\Session::_overwrite() 的参数 1 必须是数组保护函数 _overwrite(array &$old, array $new): void

This is my code:这是我的代码:

$user = $this->Auth->identify();
if ($user) {
    $this->Auth->setUser($user);
}

When I try to var_dump($user) it contains the user details.当我尝试var_dump($user)它包含用户详细信息。

Please help I can't pass in an array as the $user is not a array请帮助我无法传入数组,因为 $user 不是数组

This problems results from neither some previously generated output (in my case an warning) or other misconfiguration of your session.此问题既不是由于某些先前生成的输出(在我的情况下是警告)也不是会话的其他错误配置造成的。 When you look closer at the Session.php especially the write() method you will see this part of code:当您仔细查看 Session.php 尤其是 write() 方法时,您将看到这部分代码:

  if (!$this->started()) {
      $this->start(); //this may result in false but cakephp ignores it here... 
  }
 
$this->_overwrite($_SESSION, $data); //will fail for no session_start() has been invoked yet

So, even if your Session has not been started, the script will process further, which results in your error message.因此,即使您的 Session 尚未启动,脚本也会进一步处理,从而导致您的错误消息。 Therefore check if you may pass this one:因此检查你是否可以通过这个:

 if (ini_get('session.use_cookies') && headers_sent()) {
            
     //error headers already send
 }

As long you are not passing it, you will not be able to write any data to your session只要你不通过它,你就不能向你的会话写入任何数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM