Kal*_*zem 5 php email passwords laravel
使用Laravel 5,我需要2个不同的密码重置电子邮件视图.电子邮件视图的默认路径为emails.password.但在某些情况下,我想发送电子邮件.password_alternative.
我怎样才能做到这一点?(来自Laravel的PasswordBroker)
这是我目前的代码:
public function __construct(Guard $auth, PasswordBroker $passwords)
{
$this->auth = $auth;
$this->passwords = $passwords;
}
public function sendReset(PasswordResetRequest $request)
{
//HERE : If something, use another email view instead of the default one from the config file
$response = $this->passwords->sendResetLink($request->only('email'), function($m)
{
$m->subject($this->getEmailSubject());
});
}
Run Code Online (Sandbox Code Playgroud)
对于对Laravel 5.2感兴趣的任何人,您可以通过添加设置自定义html和文本电子邮件视图以重置密码
config(['auth.passwords.users.email' => ['auth.emails.password.html', 'auth.emails.password.text']]);
Run Code Online (Sandbox Code Playgroud)
在中间件调用之前,在构造函数中的PasswordController.php.
这将覆盖PasswordBroker的app/config/auth.php设置.
密码重置电子邮件的刀片模板位于:
yourprojectname/resources/views/auth/emails/password/html.blade.php yourprojectname/resources/views/auth/emails/password/text.blade.php
花了我很长时间.
致谢: http://ericlbarnes.com/2015/10/14/how-to-send-both-html-and-plain-text-password-reset-emails-in-laravel-5-1/ http:// academe.co.uk/2014/01/laravel-multipart-registration-and-reminder-emails/
使用PasswordBroker
并基于Illuminate/Auth/Passwords/PasswordBroker.php
类,它$emailView
是一个受保护的变量,因此一旦类实例化就无法更改该值。
但是,您有几个解决方案:
您可以创建自己的类来扩展PasswordBroker 并使用它。
class MyPasswordBroker extends PasswordBroker {
public function setEmailView($view) {
$this->emailView = $view;
}
}
// (...)
public function __construct(Guard $auth, MyPasswordBroker $passwords)
{
$this->auth = $auth;
$this->passwords = $passwords;
}
public function sendReset(PasswordResetRequest $request)
{
if ($someConditionHere) {
$this->passwords->setEmailView('emails.password_alternative');
}
$response = $this->passwords->sendResetLink($request->only('email'), function($m)
{
$m->subject($this->getEmailSubject());
});
}
Run Code Online (Sandbox Code Playgroud)您可以在方法中创建PasswordBroker,而无需使用依赖注入。
public function sendReset(PasswordResetRequest $request)
{
$emailView = 'emails.password';
if ($someConditionHere) {
$emailView = 'emails.password_alternative';
}
$passwords = new PasswordBroker(
App::make('TokenRepositoryInterface'),
App::make('UserProvider'),
App::make('MailerContract'),
$emailView
);
$response = $passwords->sendResetLink($request->only('email'), function($m)
{
$m->subject($this->getEmailSubject());
});
}
Run Code Online (Sandbox Code Playgroud)
这是一个更丑陋的解决方案,如果您有自动化测试,这将是一个痛苦的工作。
免责声明:我没有测试过任何这些代码。
归档时间: |
|
查看次数: |
4381 次 |
最近记录: |