2015-07-03 38 views
1

我有一個laravel應用程序,我想限制僅註冊到擁有公司電子郵件的特定目標組的用戶。我已經試過的東西在我的Registrar.phpLaravel註冊。只註冊擁有來自特定域的電子郵件的用戶

public function validator(array $data) 
{ 
    return Validator::make($data, [ 
     'name' => 'required|max:255', 
     'lastname' => 'required|max:255', 
     'email' => 'required|email|max:255|unique:users', 

     'password' => 'required|confirmed|min:6', 
    ]); 
} 

/** 
* Create a new user instance after a valid registration. 
* 
* @param array $data 
* @return User 
*/ 
public function create(array $data) 
{ 
    $result = null; 
    $confirmation_code = str_random(30); 

    $data = array_add($data, 'conf',$confirmation_code); 
    if(!$data['email'].ends_with(('email'), 'specificdomain.com')){ 
     Flash::message('Welcome ' .$data["name"]. '. Thanks for registering. We have sent you a validation link in your e-mail address!'); 
     Mail::send('emails.verify', $data, function($message) use ($data) { 
      $message->to($data['email'], $data['name']) 
       ->subject('Verify your email address'); 
     }); 
     $result = User::create([ 
      'name' => $data['name'], 
      'lastname' => $data['lastname'], 
      'email' => $data['email'], 
      'password' => bcrypt($data['password']), 
      'confirmation_code' => $confirmation_code 
     ]); 

    }else{ 
     $result = Flash::warning('Hi ' .$data["name"]. '. We appreciate your interest on using our System. However at the moment we offer this service only to this company!'); 
     //break; 
    } 
    return $result; 

} 

這引發以下異常

Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, Laracasts/Flash/Flash given. 

,我不能在else語句打破,因爲我得到如下:

Cannot break/continue 1 level 

顯然,我有到return Users::create([....])但要這樣做,我必須保持此塊以外的if語句。如果我這樣做,我不能檢查電子郵件域是否是必需的。所以我問,我怎樣才能把它整合到public function validator(array $data){.....}區塊?

所有的幫助表示讚賞。

回答

3

你可以擴展的電子郵件驗證您的驗證規則,如:

'email' => 'required|email|max:255|unique:users|regex:/(.*)your\.domain\.com$/i', 

(或者把它作爲一個數組,如果你需要管你的正則表達式)

可以再加入數組消息給你的驗證器,如:

$messages = array(
'email.regex' => 'We appreciate your interest on using our System. However at the moment we offer this service only to this company!', 

);

你在哪裏調用驗證添加郵件作爲第三個參數:

// Where $rules is the array you pass on now 
$validator = Validator::make($data, $rules, $messages); 

在laravel documentation你可以瞭解響應準備好一切。

您無法返回Flash。您可以使用Flash(在您的情況下)將一條消息放入會話中,該請求後將被刪除。我不完全確定你如何調用create函數以及返回的結果是什麼,但我會與此保持一致。既然你現在可以用驗證信息來解決它,你只需要刷新成功信息或錯誤。

+0

創建函數將用戶存儲在數據庫中。如果我沒有返回用戶,那麼比我在問題 – xhulio

+0

中發佈的第一個錯誤可以請您更新您的問題與您調用登錄功能的代碼?我想你應該只登錄,如果你得到一個用戶作爲結果。 –

+0

我認爲你誤解了這個問題。這是註冊部分,而不是登錄。儘管我剛剛嘗試過並按預期工作,但您的解決方案仍然有效非常感謝:) – xhulio