2016-10-02 179 views
-1

我需要在創建新用戶後發送電子郵件。Laravel電子郵件註冊確認

但我不知道如何返回到主頁沒有出現錯誤。 這就是我現在正在做的。

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 

編輯: 改變了標題,以反映答案。

+0

哪裏是錯誤的休息嗎? – Devon

+0

我現在加了。對於那個很抱歉。 – CriticalTheWizard

+1

這是RegistersUser的第63行嗎? auth :: attempt? – Devon

回答

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.'); 

    } 

} 

創建sendEmail f在使用電子郵件中構建的Laravels的相同控制器中進行聯合。請確保您創建和你的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); 
    }); 
} 

注: 你將需要在上面的例子minimium更新VIEWPATH.VIEWFILEEMAILVIEWPATH.HTMLEMAILVIEWFILE

檢查以下回購的控制器: 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

EMAIL VIEW接收變量: https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/emails/activateAccount.blade.php

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; 
+0

這是你如何創建一個用戶在你的認證控制器? – developernator

+0

是的,我現在。難道我做錯了什麼? – CriticalTheWizard

相關問題