我得到的是前一个 pass_key 值而不是当前值

Im getting the previous pass_key value instead of the present one

  1. 第一次点击忘记密码将不会在电子邮件视图中显示 pass_key,而是存储在 pass_key 字段中
  2. 第二次点击忘记密码等将会从之前的pass_key插入
  3. 中获取pass_key的值

UsersController.php

<?php
public function forgotPassword()
    {
        if ($this->request->is('post')) {
            $query = $this->Users->findByEmail($this->request->data['email']);
            $user = $query->first();
            if (is_null($user)) {
                $this->Flash->error('Email address does not exist. Please try again');
            } else {
                $passkey =Text::uuid(); // create key 
                $timeout = time() + DAY;
                $url = Router::url(['controller' => 'users', 'action' => 'reset'], true) . '/' . $passkey; // Im just generate this link in the view and pass
                $status = $this->Users->updateAll(array('Users.pass_key' => $passkey,'timeout' => $timeout), array('Users.id' => $user->id));
             if($status){

                    $this->getMailer('User')->send('forgotPasswordEmail', [$user]);             
                   $this->Flash->success(__('We already sent a link to your email'));
            return $this->redirect(['action' => 'login']);
                } else {
                    $this->Flash->error('Error saving reset passkey/timeout');
                }
              }
        }
    }
?>

UserMailer.php

<?php
namespace App\Mailer;
use Cake\Mailer\Mailer;
class UserMailer extends Mailer
{

    public function forgotPasswordEmail($user)
    {
        // attach a text file 
        $this->attachments([
            'text for user.txt'=> [
                'file'=> 'files/instruction.txt',
                    'mimetype'=>'plain/text',
                    'contentId'=>'3734hf38'
            ],


        // attach an image file 
            'edit.png'=>[
                'file'=>'files/ourlogo.png',
                    'mimetype'=>'image/png',
                    'contentId'=>'734h3r38'
                ]
        ])
        ->to($user->email)
        ->emailFormat('html')
        ->subject(sprintf('Forgot Password link  %s', $user->username))
        ->viewVars([
        'username'=> $user->username,
        'useremail'=>$user->email,
        'passkey' => $user->pass_key,
        'userid' => $user->id
        ])

        // the template file you will use in this emial
        ->template('forgotPasswordEmail') // By default template with same name as method name is used.
        // the layout .ctp file you will use in this email
        ->layout('customLayout');
    }
}
?>

Template\Email\html\forgot_password_email.ctp

<?php
use Cake\Routing\Router;
?>

<p>Your username is:  <?=$username?> </p>

<p>Click on the link below to Reset Your Password.</p>

<?php $resetUrl = Router::url(['controller' => 'users', 'action' => 'reset/' . $passkey]);?>

<p><a href="http://localhost<?php echo $resetUrl; ?>">Click here to Reset Your Password</a></p>

?>




  <?php
        $url = Router::url(['controller' => 'users', 'action' => 'reset'], true) . '/' . $passkey; // Im just generate this link in the view and pass
                        $status = $this->Users->updateAll(array('Users.pass_key' => $passkey,'timeout' => $timeout), array('Users.id' => $user->id));
                     if($status){

        var_dump($url);exit;  I get the exact pass_key but I dont know how to pass the url to forgot_password_email.ctp . I just know how to pass the using this [$user] 


 var_dump($user->pass_key);exit; will retrieve the previous transaction not current pass_key

        .............
    ?>

你的问题是你正在生成密钥并将其保存到数据库,但没有更新 $user 对象,所以当你将它传递给视图时它仍然有旧版本。

试试这个:

$this->Users->patchEntity($user, [
    'pass_key' => Text::uuid(), // create key 
    'timeout' => time() + DAY,
]);
if ($this->Users->save($user)) {

在这里,我们用新值更新 $user 实体,以便它们存在于视图中,并使用 save 函数而不是 updateAll

另外,在您看来,您应该改为:

$resetUrl = Router::url(['controller' => 'users', 'action' => 'reset', $passkey]);