如何使用 CakeDC 用户插件检查用户是否登录到 cakephp 4 应用程序?

How to Check if user is logged in cakephp 4 application using CakeDC users plugin?

如何使用 CakeDC 用户插件检查用户是否登录到 cakephp 4 应用程序? 我需要在用户登录时更改主题

在Application.php

public function bootstrap(): void
{
    $this->addPlugin('BootstrapUI');

    // Call parent to load bootstrap from files.
    parent::bootstrap();
    $this->addPlugin('CakeDC/Users', ['routes' => true, 'bootstrap' => true]);
    //$this->addPlugin(\CakeDC\Users\Plugin::class);
    Configure::write('Users.config', ['users']);
}

在AppController.php

public function beforeFilter(EventInterface  $event)
    {
        parent::beforeFilter($event);
           $user = $this->Authentication->identify();
        if ($user) {    
              $this->viewBuilder()->setlayout( 'CakeLte.default' );

        }
}

这个错误

假设您已经在您的应用中正确配置了身份验证组件,您可以检查用户是否已连接,如下所示:

if($this->Authentication->getIdentity()) {
  // user logged
} else {
  // user not logged
}

以防万一,这是一些对您有用的代码示例。

在你的控制器中:

$this->Authentication->getIdentity();
// Access to a field
$this->Authentication->getIdentity()->username;
// Access to a method of the entity
$this->Authentication->getIdentity()->isAdmin();

在您看来:

$this->request->getAttribute('identity')
// Access to a field
$this->request->getAttribute('identity')->username;
// Access to a method of the entity
$this->request->getAttribute('identity')->getOriginalData()->isAdmin();

我承认这很难找到,文档看起来像是放弃了一些功能。