使用Laravel 5,我需要2个不同的 View 来进行密码重置电子邮件。电子邮件 View 的默认路径是 emails.password 。但是在某些情况下,我想发送 emails.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());
});
}
最佳答案
使用 PasswordBroker
并基于 Illuminate/Auth/Passwords/PasswordBroker.php
类,$emailView
是一个 protected 变量,因此一旦实例化该类,就无法更改该值。
但是,您有两种解决方案:
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());
});
}
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());
});
}
这是一个较丑陋的解决方案,如果您具有自动化测试,将很难使用。
免责声明:我尚未测试任何此代码。
关于php - 更改电子邮件查看路径以在Laravel上重置密码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36241673/