Check current user Auth ID in view

Hi all,

Can you please let me know if the below is the proper way to list all users except the one currently logged in the view ?

<?php //Listing all users
                            foreach($users as $user) :
                                if($user->id != $this->request->getSession()->read('Auth')->id):
                                ?>
                                    <tr>
                                        <td class="align-middle"><?= $user->firstname ?></td>
                                        <td class="align-middle"><?= $user->lastname ?></td>
                                        <td class="align-middle"><?= $user->email ?></td>
                                        <td class="align-middle"><?= $user->role ?></td>
                                        <td class="align-middle"><?= $user->created->format('d/m/Y') ?></td>
                                        <td class="align-middle">
                                            <?= $this->Html->link('<i class="far fa-trash-alt"></i>', ['action' => 'delete', $user->id], ['class' => 'btn btn-danger', 'escape' => false]) ?>
                                        </td>
                                    </tr>
                                <?php endif; ?>
                            <?php endforeach; ?>

You should do the selection in the controller, not in the view

Also, the “proper” way will depend on what version of CakePHP you’re running, and whether you’re using the new authentication plugin or the old component.