在CakePHP 3中重置密码通常涉及以下几个步骤:
首先,你需要在 config/routes.php
文件中设置一个路由来处理密码重置请求。
// config/routes.php
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;
Router::defaultRouteClass(DashedRoute::class);
Router::scope('/', function (RouteBuilder $routes) {
// 其他路由...
// 密码重置路由
$routes->connect('/reset-password', ['controller' => 'Users', 'action' => 'resetPassword']);
});
在你的用户控制器(通常是 src/Controller/UsersController.php
)中创建一个处理密码重置的动作。
// src/Controller/UsersController.php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\EventInterface;
use Cake\Utility\Security;
class UsersController extends AppController
{
public function initialize(): void
{
parent::initialize();
$this->loadComponent('Security');
}
public function resetPassword()
{
if ($this->request->is(['post', 'put'])) {
$token = $this->request->getData('token');
$user = $this->Users->find()
->where(['reset_token' => $token])
->first();
if ($user) {
$user->password = $this->request->getData('password');
$user->reset_token = null; // 清除重置令牌
if ($this->Users->save($user)) {
$this->Flash->success(__('Your password has been reset.'));
return $this->redirect(['controller' => 'Users', 'action' => 'login']);
} else {
$this->Flash->error(__('Unable to reset your password.'));
}
} else {
$this->Flash->error(__('Invalid or expired token.'));
}
}
}
}
创建一个视图文件来显示密码重置表单。通常这个文件位于 src/Template/Users/reset_password.ctp
。
// src/Template/Users/reset_password.ctp
<div class="users form">
<?= $this->Form->create() ?>
<fieldset>
<legend><?= __('Reset Password') ?></legend>
<?= $this->Form->hidden('token', ['value' => $token]) ?>
<?= $this->Form->input('password', ['type' => 'password']) ?>
<?= $this->Form->input('confirm_password', ['type' => 'password']) ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
通常,用户会通过电子邮件收到一个包含重置链接的邮件。这个链接应该包含一个唯一的令牌,用于验证用户的身份并允许他们重置密码。
在你的用户控制器中添加一个动作来处理发送重置邮件的请求。
// src/Controller/UsersController.php
public function requestResetPassword()
{
if ($this->request->is(['post', 'put'])) {
$email = $this->request->getData('email');
$user = $this->Users->find()
->where(['email' => $email])
->first();
if ($user) {
$token = Security::randomBytes(32);
$hash = hash('sha256', $token);
$user->reset_token = $hash;
$this->Users->save($user);
// 发送邮件逻辑(使用CakePHP的邮件组件或其他邮件服务)
// ...
$this->Flash->success(__('An email has been sent with instructions to reset your password.'));
} else {
$this->Flash->error(__('Email not found.'));
}
}
}
确保你已经配置了CakePHP的邮件组件,并在你的 config/app.php
中启用了它。
// config/app.php
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
'host' => 'smtp.example.com',
'port' => 587,
'username' => 'user@example.com',
'password' => 'secret',
'tls' => true,
],
],
'Email' => [
'default' => [
'transport' => 'default',
'from' => ['noreply@example.com' => 'My App'],
],
],
创建一个邮件模板来包含重置密码的链接。
// src/Template/Email/html/reset_password.ctp
<p>Hello <?= h($user->name) ?>,</p>
<p>You have requested to reset your password. Please click the link below to proceed:</p>
<p><a href="<?= $this->Url->build(['controller' => 'Users', 'action' => 'resetPassword', 'token' => $user->reset_token], true) ?>">Reset Password</a></p>
<p>If you did not request this, please ignore this email.</p>
领取专属 10元无门槛券
手把手带您无忧上云