cakephp 如何检索已连接用户的角色并将其显示在 Jmeter 板上?

vuktfyat  于 2023-06-06  发布在  PHP
关注(0)|答案(1)|浏览(245)

我想显示与CakePHP4连接的用户的角色,但我找不到如何做到这一点,我已经可以显示他的名字和其他人,但对于角色的名称,它不起作用,我只是可以访问它'在我的用户表中的角色的ID
在我 Jmeter 板的侧边栏视图上

<?php $administrateur = $this->request->getAttribute('identity'); ?>
     <h6 class="mb-0"><?= $administrateur->pseudo?></h6>
     <span>
     <?= $administrateur->role->nom ?>
    </span>
6kkfgxo0

6kkfgxo01#

我使用cakephp 4.3和cakephp/authentication 2.9插件。(https://book.cakephp.org/authentication/2/en/authentication-component.html
我在控制器操作中请求登录的用户数据。

public function view(){
$user = $this->Authentication->getIdentity();
$this->set(compact('user'));
}

我更喜欢使用$this->Authentication->getIdentity();,因为结果对我来说比$this->request->getAttribute('identity');更容易使用
您可以在视图模板中debug($user);,查看它包含哪些数据,并可以简单地访问它们。我可以通过以下方式访问视图模板中的角色

<?= $user->role =>

如果你想在一个视图中显示角色,上面的例子就可以了。
如果你想在多个页面上显示它,就像在菜单中一样,我建议阅读有关ViewCells的内容:https://book.cakephp.org/4/en/views/cells.html#creating-a-cell

相关问题