使用cakephp 3.4将URL链接从用户ID更改为用户名后,无法编辑我的个人资料

时间:2018-01-29 04:20:45

标签: cakephp cakephp-3.0

我有这种方法在网址http://localhost/sample/users/profile/john而不是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']);
}

当我尝试编辑我的个人资料时,它将始终进入"您不能这样做。"

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导致问题?

1 个答案:

答案 0 :(得分:0)

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

只需修改个人资料.ctp并将$ users-&gt; 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>

解释你总是直接说“你不允许这样做”。因为这在个人资料方法

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

由于您在用户表的数据库中存在重复的用户名,系统会混淆要编辑的配置文件,因此它会抛出错误“您不允许这样做”。找到具有相同“用户名值”的第一行数据后

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

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