使用 cakephp 3.4 将 url link 从用户 ID 更改为用户名后无法编辑我的个人资料

Cannot edit my profile after changing the url link from user id to username using cakephp 3.4

我有这种方法可以在 url http://localhost/sample/users/profile/john instead of http://localhost/sample/users/view/1

中显示像这样的用户个人资料
public function profile($username)
{
    $user = $this->Users->find()->where(['username' => $username])->first(); 
    $accountUsername  =  $user->username;
    $this->set('profileUserName', $accountUsername);
    $this->set('users', $user);
    $this->set('_serialize', ['user']);
}

当我尝试编辑我的个人资料时,它总是会转到 "You are not allowed to do this."

public function edit($id = null)
{
  $logged_user_id=$this->Auth->user('id');
  if($logged_user_id==$id){
      $user = $this->Users->get($id, [
        'contain' => []
    ]);
      if ($this->request->is(['patch', 'post', 'put'])) {
        $user = $this->Users->patchEntity($user, $this->request->getData());


        if ($this->Users->save($user)) {
            $this->Flash->success(__('User profile successfuly  updated.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }

    }
    $this->set(compact('user'));
    $this->set('_serialize', ['user']);
} else {
    $this->Flash->error(__('You are not allowed to do this.'));
    return $this->redirect(['action' => 'index']);
}
}

我试图在编辑方法中添加这个

$logged_user_id=$this->Auth->user('id');
$logged_user_name=$this->Auth->user('username');

  if(($logged_user_id==$id)&&($logged_user_name == $username)){
      $user = $this->Users->get($id, [
        'contain' => []
    ]);

profile.ctp

<div class="paginator">
    <ul>       
        <li><?= $this->Html->link(__('Edit User'), ['action' => 'edit', $users->id]) ?> </li>
        <li><?= $this->Form->postLink(__('Delete User'), ['action' => 'delete', $users->id], ['confirm' => __('Are you sure you want to delete # {0}?', $users->id)]) ?> </li>
        <li><?= $this->Html->link(__('List Users'), ['action' => 'index']) ?> </li>

        <li><?= $this->Html->link(__('Logout'), ['action' => 'logout']) ?> </li>
    </ul>   
  </div>

可能是因为 $id 的获取导致了问题?

public function beforeFilter(\Cake\Event\Event $event)
{
  $user = $this->request->session()->read('Auth.User');
  $this->set('user_id', $user['id']); 
}

只需编辑您的 profile.ctp 并将 $users->id 更改为 $user_id

<div class="paginator">
    <ul>       
        <li><?= $this->Html->link(__('Edit User'), ['action' => 'edit', $user_id]) ?> </li>
        <li><?= $this->Form->postLink(__('Delete User'), ['action' => 'delete', $users->id], ['confirm' => __('Are you sure you want to delete # {0}?', $users->id)]) ?> </li>
        <li><?= $this->Html->link(__('List Users'), ['action' => 'index']) ?> </li>

        <li><?= $this->Html->link(__('Logout'), ['action' => 'logout']) ?> </li>
    </ul>   
  </div>

解释你总是直接去 "You are not allowed to do this." 因为这个在配置文件方法

$user = $this->Users->find()->where(['username' => $username])->first();

由于您在数据库中为用户 table 设置了重复的用户名,因此系统混淆了要编辑的配置文件,因此在找到第一行具有相同 "You are not allowed to do this." 的数据后抛出错误 "You are not allowed to do this." =24=]

将此代码添加到 UsersTable.php 以防止重复的用户名

$validator
        ->requirePresence('username')
        ->notBlank('username', 'A username is required')
        ->add('username', 'unique', [
                    'rule' => 'validateUnique',
                    'provider' => 'table',
                    'message' => 'Username is already used'
             ]);