<article><h1>Laravel 的密码重设功能</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#resetting-database">数据库注意事项</a></li><li><a href="#resetting-routing">路由</a></li><li><a href="#resetting-views">视图</a></li><li><a href="#after-resetting-passwords">重置密码后</a></li><li><a href="#password-customization">密码自定义</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> <strong>想要快速上手此功能?</strong> 首先在 Laravel 应用下运行 <code class=" language-php">php artisan make<span class="token punctuation">:</span>auth</code> 命令,然后使用浏览器打开 <code class=" language-php">http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>your<span class="token operator">-</span>app<span class="token punctuation">.</span>dev<span class="token operator">/</span>register</code> ,或者任意一个在应用中分配的 URL 。这个命令将会生成包括密码重置在内的整个认证系统。</p></blockquote><p>大部分的 web 应用都为用户提供重置密码的功能。Laravel 提供了一种非常方便的方法用于发送密码重置邮件来完成密码重置,而不需要在每个应用中重新实现。</p><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 在使用 Laravel 密码重置功能之前, 你必须 <code class=" language-php"><span class="token keyword">use</span></code> 这个 <code class=" language-php">Illuminate\<span class="token package">Notifications<span class="token punctuation">\</span>Notifiable</span></code> <code class=" language-php"><span class="token keyword">trait</span></code>。</p></blockquote><p><a name="resetting-database"></a></p><h2><a href="#resetting-database">数据库注意事项</a></h2><p>开始之前,请先确认你的 <code class=" language-php">App\<span class="token package">User</span></code> 模型是否实现了 <code class=" language-php">Illuminate\<span class="token package">Contracts<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>CanResetPassword</span></code> 协议。当然,在 Laravel 框架中 <code class=" language-php">App\<span class="token package">User</span></code> 模型已经实现了这个接口,并使用 <code class=" language-php">Illuminate\<span class="token package">Auth<span class="token punctuation">\</span>Passwords<span class="token punctuation">\</span>CanResetPassword</span></code> <code class=" language-php"><span class="token keyword">trait</span></code> 来实现该接口包含的方法。</p><h4>生成重置令牌表迁移</h4><p>接下来,我们必须先创建用来存储密码重置令牌的数据表。由于 Laravel 已经自带了这张表的迁移,就存放在 <code class=" language-php">database<span class="token operator">/</span>migrations</code> 目录,因此,你仅仅只需要运行下面的命令即可完成数据表的创建:</p><pre class=" language-php"><code class=" language-php">php artisan migrate</code></pre><p><a name="resetting-routing"></a></p><h2><a href="#resetting-routing">路由</a></h2><p>Laravel 在 <code class=" language-php">Auth\<span class="token package">ForgotPasswordController</span></code> 和 <code class=" language-php">Auth\<span class="token package">ResetPasswordController</span></code> 两个类中包含了电子邮件密码重置链接和重置用户密码的必要逻辑。你只需要使用 Artisan 命令 <code class=" language-php">make<span class="token punctuation">:</span>auth</code> 命令即可生成密码重置所需要的路由:</p><pre class=" language-php"><code class=" language-php">php artisan make<span class="token punctuation">:</span>auth</code></pre><p><a name="resetting-views"></a></p><h2><a href="#resetting-views">视图</a></h2><p>和路由一样Laravel 在执行 <code class=" language-php">make<span class="token punctuation">:</span>auth</code> 命令时同时会在 <code class=" language-php">resources<span class="token operator">/</span>views<span class="token operator">/</span>auth<span class="token operator">/</span>passwords</code> 生成密码重置必要视图文件。当然,你可以随意对它们进行修改。</p><p><a name="after-resetting-passwords"></a></p><h2><a href="#after-resetting-passwords">重置密码后</a></h2><p>一旦您定义了重置用户密码的路由和视图后,您可以在浏览器中访问 <code class=" language-php"><span class="token operator">/</span>password<span class="token operator">/</span>reset</code> 。 框架自带的 <code class=" language-php">ForgotPasswordController</code> 已经包含了发送密码重置链接邮件的逻辑, <code class=" language-php">ResetPasswordController</code> 包含了重置用户密码的逻辑。</p><p>在密码重置之后,用户将会自动登录并重定向到 <code class=" language-php"><span class="token operator">/</span>home</code> 。你可以定义 <code class=" language-php">ResetPasswordController</code> 控制器的 <code class=" language-php">redirectTo</code> 属性来定制密码重置成功后的自定义跳转链接:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">protected</span> <span class="token variable">$redirectTo</span> <span class="token operator">=</span> <span class="token string">'/dashboard'</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 默认情况下, 密码重置令牌在一小时内有效。你可以通过修改 <code class=" language-php">config<span class="token operator">/</span>auth<span class="token punctuation">.</span>php</code> 的 <code class=" language-php">expire</code> 选项来修改此项配置。</p></blockquote><p><a name="password-customization"></a></p><h2><a href="#password-customization">自定义</a></h2><h4>自定义认证 Guard</h4><p>在配置文件 <code class=" language-php">auth<span class="token punctuation">.</span>php</code> 中, 你可以配置多个 「guards」 参数,可以用来多用户表的独立认证。你可以通过重写 <code class=" language-php">ResetPasswordController</code> 控制器的 <code class=" language-php">guard</code> 方法来实现自定义认证 <code class=" language-php">guard</code> , 同时别忘了,这个方法需要返回一个 <code class=" language-php">guard</code> 实例:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Auth</span><span class="token punctuation">;</span>
<span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function">guard<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">guard<span class="token punctuation">(</span></span><span class="token string">'guard-name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><h4>自定义密码 Broker</h4><p>在配置文件 <code class=" language-php">auth<span class="token punctuation">.</span>php</code> 中, 可以配置多个密码 「brokers」 , 它可以用于多个用户表重置密码。你可以通过重写 <code class=" language-php">ForgotPasswordController</code> 和 <code class=" language-php">ResetPasswordController</code> 控制器的 <code class=" language-php">broker</code> 方法来实现你的自定义 <code class=" language-php">broker</code> :</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Password</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* 获取在密码重置期间使用的 broker
*
* @return PasswordBroker
*/</span>
<span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function">broker<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token scope">Password<span class="token punctuation">::</span></span><span class="token function">broker<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><h4>自定义重置邮箱</h4><p>你可以方便的修改通知类用以给用户发送密码重置链接。在开始之前, 重写 <code class=" language-php">User</code> 模型中 <code class=" language-php">sendPasswordResetNotification</code> 方法。在这个方法中,你可以使用你选择的任意通知类发送通知。这个方法的第一个参数为密码重置令牌 <code class=" language-php"><span class="token variable">$token</span></code> :</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 发送密码重置通知
*
* @param string $token
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">sendPasswordResetNotification<span class="token punctuation">(</span></span><span class="token variable">$token</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">notify<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">ResetPasswordNotification</span><span class="token punctuation">(</span><span class="token variable">$token</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="https://github.com/GanymedeNil">@GanymedeNil</a></td><td><img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/6859_1487055454.jpg?imageView2/1/w/100/h/100"></td><td>翻译</td><td>我不是Full Stack Developer 2333 <a href="http://weibo.com/jinhongyang">@GanymedeNil</a></td></tr></tbody></table></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度