Laravel 5忘记密码电子邮件模板条件


Laravel 5 forgotten password email template condition

我使用的是Laravel 5内置的用户属性和Entrust,用于用户角色和权限。我设置了两个角色,即管理员和用户。基本上,我想做的是有两个不同的忘记密码电子邮件模板——一个为用户,一个为管理员。因此,当用户输入他们的电子邮件地址以将重置链接通过电子邮件发送给他们时,我需要首先检查他们是哪种类型的用户,然后向他们发送正确的模板。我不想在标准电子邮件模板中做任何棘手的事情——他们肯定是在控制器中做这件事的方法吗?有人知道我会怎么做吗?

您可能会提示他们输入电子邮件,当他们提交时,您可以在控制器中获取:

public function forgot()
{
    $email = Input::get('email');
    $user = User::where('email', $email)->first();
    if($user->type == 'admin') {
        // logic to email admin with admin template
    } else {
        // logic to email user with user template 
    }
    // redirect to success page letting user know the email was sent
    return View::make('someview');
}

或者更好的是,只需将用户类型传递给处理电子邮件的电子邮件服务:

public function forgot()
{
   $email = Input::get('email');
   $user = User::where('email', $email)->first();
   $emailService->sendForgotForType($user->type);
   // redirect to success page letting user know the email was sent
   return View::make('someview');
}

如果您使用的是Laravel 5内置的用户管理:

要覆盖使用的默认模板,您需要通过编写一个扩展PasswordBroker的新类来手动设置PasswordBroker.php中的$emailView

例如,在config/app.php 中注释掉'Illuminate'Auth'Passwords'PasswordResetServiceProvider'

然后创建一个扩展类:

use Illuminate'Contracts'Auth'PasswordBroker;
use Illuminate'Contracts'Auth'CanResetPassword;
class MyPasswordBroker extends PasswordBroker {
    // override 
    public function emailResetLink(CanResetPasswordContract $user, $token, Closure $callback = null)
    {
        // Override Logic to email reset link function perhaps using the example above?
    }
}

然后,您需要在register方法中将新的MyPasswordBroker类绑定到app/Providers/AppServiceProvider.php处的AppServiceProvider(下面在线找到):

$this->app->bind('App'Model'PasswordBroker', function($app) {
    $key = $app['config']['app.key'];
    $userToken = new 'App'Model'NewUserToken; 
    $tokens = new 'App'Repository'NewTokenRepository($key,$userToken);
    $user = new 'App'Model'NewUser;
    $view = $app['config']['auth.password.email'];
    return new 'App'Model'PasswordBroker($tokens, $users, $app['mailer'], $view);
});

如果你能处理的话,绝对是中等程度的高级东西——太棒了。否则,我可能会考虑使用具有所需内置功能的身份验证包。