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

时间:2015-04-08 18:03:28

标签: php laravel laravel-5

我正在使用带有Entrust的内置用户资源的Laravel 5来获取用户角色和权限。我设置了两个角色,分别是管理员和用户。基本上我想要做的是有两个不同的忘记密码电子邮件模板 - 一个用于用户,一个用于管理员。因此,当用户输入他们的电子邮件地址以通过电子邮件发送给他们时,我需要先检查他们是哪种用户,然后向他们发送正确的模板。我不想在标准电子邮件模板中做任何类型的hacky东西,他们必须是在控制器中做到这一点的方式或肯定的东西?有谁知道我会怎么做?

1 个答案:

答案 0 :(得分:1)

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

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内置的用户管理:

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

例如,在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?
    }
}

然后,您需要在注册方法{下面找到MyPasswordBroker的新AppServiceProvider课程中绑定app/Providers/AppServiceProvider.php

$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);
});

绝对适度的高级东西,如果你能处理它 - 很棒。否则,我可能会考虑使用具有您需要的内置功能的身份验证包。

相关问题