-1

创建新用户后,我需要发送电子邮件。

但是我不知道如何返回主页而不出现错误。这就是我现在正在做的事情。

User::create([
            'name' => $data['name'],
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'phone' => bcrypt($data['phone']),
            'confirmation_code' => str_random(30),
        ]);

Email_function();

if (Auth::attempt(['email' => $data['email'], 'password' => bcrypt($data['password']) ])) {
        // Authentication passed...
        return redirect('/');
    }

我不断收到这个作为我的错误信息。

SErrorException in SessionGuard.php line 439:
Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in /Applications/XAMPP/xamppfiles/htdocs/sniddl/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php on line 63 and defined

编辑: 更改标题以反映答案。

4

2 回答 2

4

这是一个修改后的创建功能,为您的注册控制器添加了电子邮件功能

确保请求包含在使用命名空间的页面顶部:

use Illuminate\Http\Request;

更改控制器中的创建功能:

protected function create(Request $data)
{


    $user                       = new User;
    $user->name                 = $data->input('name');
    $user->username             = $data->input('username');
    $user->email                = $data->input('email');
    $user->password             = bcrypt($data->input('password'));
    $user->phone                = bcrypt($data->input('phone'));
    $user->confirmation_code    = str_random(60);
    $user->save();

    if ($user->save()) {

        $this->sendEmail($user);

        return redirect('VIEWPATH.VIEWFILE')->with('status', 'Successfully created user.');

    } else {

        return redirect('VIEWPATH.VIEWFILE')->with('status', 'User not created.');

    }

}

在将使用 Laravel 内置电子邮件的同一控制器中创建 sendEmail 函数。确保您创建了 HTML 电子邮件:

public function sendEmail(User $user)
{
    $data = array(
        'name' => $user->name,
        'code' => $user->confirmation_code,
    );
    \Mail::queue('EMAILVIEWPATH.HTMLEMAILVIEWFILE', $data, function($message) use ($user) {
        $message->subject( 'Subject line Here' );
        $message->to($user->email);
    });
}

注意:您需要在上面的示例中至少更新VIEWPATH.VIEWFILE和。EMAILVIEWPATH.HTMLEMAILVIEWFILE

检查下面的 CONTROLLER 存储库: https ://github.com/laravel/laravel/blob/master/app/Http/Controllers/Auth/RegisterController.php https://github.com/jeremykenedy/laravel-auth/blob/ master/app/Http/Controllers/Auth/AuthController.php

注册视图(刀片)示例 https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/auth/register.blade.php

接收变量的电子邮件视图: https ://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/emails/activateAccount.blade.php

于 2016-10-03T01:42:29.927 回答
0

好的,事实证明,通过设置User::create为变量,它允许您通过返回变量来登录用户。像这样。

$user = User::create([
            'name' => $data['name'],
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'phone' => bcrypt($data['phone']),
            'confirmation_code' => str_random(30),
        ]);

Email_function();

return $user;
于 2016-10-02T20:44:20.723 回答